> For the complete documentation index, see [llms.txt](https://docs.glesys.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.glesys.com/products/storage/block-storage/how-tos/stop-a-systemd-service-when-a-volume-is-detached-linux.md).

# Stop a systemd service when a volume is detached (Linux)

To avoid data loss if a volume is detached via the control panel, you can automatically stop the service that depends on it through its systemd service file.

***

In Linux, it's possible to stop a service that depends on a volume, assuming the service is controlled by `systemd`. This will help prevent data loss in case you accidentally detach the wrong volume.

This will be demonstrated with a simple service writing the current date and time to a text file on a volume. Once the volume is detached, the service will be stopped automatically.

## The example setup

In this setup, a volume with the WWN `3fa0d41402141ef7` is mounted on `/mnt/volume1` on a Debian VM.

The program that writes the current date and time to a file is a simple Bash script that looks like below. The file is called `date-writer.sh` and is placed in `/usr/local/sbin/` by root.

{% code title="/usr/local/sbin/date-writer.sh" %}

```bash
#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin

while true; do
    date >> /mnt/volume1/date-output.txt
    sleep 1
done
```

{% endcode %}

The `systemd` service file for the above script is called `date-writer.service` and is placed in `/etc/systemd/system/` (by root). The two important lines are `RequiresMountsFor=/mnt/volume1` and `BindsTo=mnt-volume1.mount`.

The `mnt-volume1.mount` file is automatically created by `systemd` for the volume. Systemd creates its own mounts for all mounts in `/etc/fstab`. To figure out the correct filename, replace all slashes (`/`) except the first with a hyphen (`-`) and add a `.mount` suffix. In this example, `/mnt/volume1` becomes `mnt-volume1.mount` for `systemd`. If you have trouble figuring out the systemd name of the mount, you can take a look inside the `/run/systemd/generator/` directory.

<pre class="language-systemd" data-title="/etc/systemd/system/date-writer.sh"><code class="lang-systemd">[Unit]
Description=Write the date to a file
<a data-footnote-ref href="#user-content-fn-1">RequiresMountsFor=/mnt/volume1</a>
<a data-footnote-ref href="#user-content-fn-2">BindsTo=mnt-volume1.mount</a>
[Service]
ExecStart=/usr/local/sbin/date-writer.sh
[Install]
WantedBy=multi-user.target
</code></pre>

Once the above service file is placed in `/etc/systemd/system/` you need to reload the `systemd` daemon to make it recognize the new file.

<pre data-title="Command"><code><strong>sudo systemctl daemon-reload
</strong></code></pre>

Enable and start the service.

<pre data-title="Commands and output. Commands are highlighted."><code><strong>sudo systemctl enable date-writer.service
</strong>Created symlink '/etc/systemd/system/multi-user.target.wants/date-writer.service' → '/etc/systemd/system/date-writer.service
<strong>sudo systemctl start date-writer.service
</strong></code></pre>

Double-check that the service is running.

<pre data-title="Command and output. Command is highlighted."><code><strong>sudo systemctl is-active date-writer.service
</strong>active
</code></pre>

Check if the script is writing the current date and time to the volume.

<pre data-title="Command and output. Command is highlighted."><code><strong>tail /mnt/volume1/date-output.txt
</strong>Tue Oct 13 13:29:41 UTC 2026
Tue Oct 13 13:29:42 UTC 2026
Tue Oct 13 13:29:43 UTC 2026
Tue Oct 13 13:29:44 UTC 2026
Tue Oct 13 13:29:45 UTC 2026
Tue Oct 13 13:29:46 UTC 2026
Tue Oct 13 13:29:47 UTC 2026
Tue Oct 13 13:29:48 UTC 2026
Tue Oct 13 13:29:49 UTC 2026
Tue Oct 13 13:29:50 UTC 2026
</code></pre>

## Testing

{% hint style="warning" %}
Don't run these tests on a production service! This is just to make sure the service stops when the volume gets detached.
{% endhint %}

Navigate to **Block storage → Storage** in the control panel and click on the volume that is mounted on `/mnt/volume1`. Click **Detach**.

<figure><img src="https://3379166260-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlMZhhdQNcWOpX5Bj48GK%2Fuploads%2FSO3r44iGVES5wFSBKvRr%2Fblock-storage-detach-test.png?alt=media&amp;token=01c79c61-b07f-42c9-b3dc-c67d3797ac2a" alt=""><figcaption></figcaption></figure>

Confirm the detach in the next dialog box by clicking **Detach**.

<figure><img src="https://3379166260-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlMZhhdQNcWOpX5Bj48GK%2Fuploads%2FPcmkjEYaOr1BzybuRgnE%2Fblock-storage-detach2.png?alt=media&amp;token=a59b1f7b-93d4-4eab-8c6c-5bb338b2868a" alt=""><figcaption></figcaption></figure>

Check if the `date-writer.service` is running. It should be stopped by now.

<pre data-title="Command and output. Command is highlighted."><code><strong>sudo systemctl is-active date-writer.service
</strong>inactive
</code></pre>

The service is indeed stopped (inactive). No more lines should be written to the `date-output.txt` file.

Once the volume is attached again, you need to manually mount it with `sudo mount /mnt/volume1`. You also need to restart the service with `sudo systemctl restart date-writer.service` once the volume is mounted.

The same thing will happen if you manually unmount the volume using `sudo umount /mnt/volume1`. The service will be instantly stopped to make sure no data is written to the directory `/mnt/volume1` when nothing is mounted on it.

[^1]: This line makes the service depend on the volume. If the volume is unmounted, the service is stopped.

[^2]: This is bit stricter and will stop the service if anything happens to the systemd mount for the volume.
