Skip to main content
Commonmark migration
Source Link

Systemd Unit Start Conditions

With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

 

ConditionPathIsMountPoint= is similar to ConditionPathExists= but verifies whether a certain path exists and is a mount point.

Seems like the easiest method is to include

ConditionPathIsMountPoint=/mnt/backup

In your [Unit] section. Though this doesn't check which device is mounted there, though in the case of creating a backup the user may not be picky.

You'd probably also need Restart=on-failure to continue trying to start the service.


A more precise method would be to write a udev rule that starts the service when a particular device is mounted.

As a reference

Start by finding your device in lsusb. Note the ID (eg 0a81:0101)

 

Create a new udev rules file in /etc/udev/rules.d/ via sudoedit /etc/udev/rules.d/100-mount-videos.rulesand plonk a new rule in there like this:

 

ACTION=="add", ATTRS{idVendor}=="0a81", ATTRS{idProduct}=="0101", RUN+="/home/your_username/bin/mount_videos.sh"

You could just replace the RUN+= with RUN+=/usr/bin/systemctl start backup.service or whatever you called your service

Systemd Unit Start Conditions

With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

 

ConditionPathIsMountPoint= is similar to ConditionPathExists= but verifies whether a certain path exists and is a mount point.

Seems like the easiest method is to include

ConditionPathIsMountPoint=/mnt/backup

In your [Unit] section. Though this doesn't check which device is mounted there, though in the case of creating a backup the user may not be picky.

You'd probably also need Restart=on-failure to continue trying to start the service.


A more precise method would be to write a udev rule that starts the service when a particular device is mounted.

As a reference

Start by finding your device in lsusb. Note the ID (eg 0a81:0101)

 

Create a new udev rules file in /etc/udev/rules.d/ via sudoedit /etc/udev/rules.d/100-mount-videos.rulesand plonk a new rule in there like this:

 

ACTION=="add", ATTRS{idVendor}=="0a81", ATTRS{idProduct}=="0101", RUN+="/home/your_username/bin/mount_videos.sh"

You could just replace the RUN+= with RUN+=/usr/bin/systemctl start backup.service or whatever you called your service

Systemd Unit Start Conditions

With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

ConditionPathIsMountPoint= is similar to ConditionPathExists= but verifies whether a certain path exists and is a mount point.

Seems like the easiest method is to include

ConditionPathIsMountPoint=/mnt/backup

In your [Unit] section. Though this doesn't check which device is mounted there, though in the case of creating a backup the user may not be picky.

You'd probably also need Restart=on-failure to continue trying to start the service.


A more precise method would be to write a udev rule that starts the service when a particular device is mounted.

As a reference

Start by finding your device in lsusb. Note the ID (eg 0a81:0101)

Create a new udev rules file in /etc/udev/rules.d/ via sudoedit /etc/udev/rules.d/100-mount-videos.rulesand plonk a new rule in there like this:

ACTION=="add", ATTRS{idVendor}=="0a81", ATTRS{idProduct}=="0101", RUN+="/home/your_username/bin/mount_videos.sh"

You could just replace the RUN+= with RUN+=/usr/bin/systemctl start backup.service or whatever you called your service

added 689 characters in body
Source Link
Centimane
  • 4.5k
  • 3
  • 25
  • 47

Systemd Unit Start Conditions

With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

ConditionPathIsMountPoint= is similar to ConditionPathExists= but verifies whether a certain path exists and is a mount point.

Seems like the easiest method is to include

ConditionPathIsMountPoint=/mnt/backup

In your [Unit] section. Though this doesn't check which device is mounted there, though in the case of creating a backup the user may not be picky.

You'd probably also need Restart=on-failure to continue trying to start the service.


A more precise method would be to write a udev rule that starts the service when a particular device is mounted.

As a reference

Start by finding your device in lsusb. Note the ID (eg 0a81:0101)

Create a new udev rules file in /etc/udev/rules.d/ via sudoedit /etc/udev/rules.d/100-mount-videos.rulesand plonk a new rule in there like this:

ACTION=="add", ATTRS{idVendor}=="0a81", ATTRS{idProduct}=="0101", RUN+="/home/your_username/bin/mount_videos.sh"

You could just replace the RUN+= with RUN+=/usr/bin/systemctl start backup.service or whatever you called your service

Systemd Unit Start Conditions

With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

ConditionPathIsMountPoint= is similar to ConditionPathExists= but verifies whether a certain path exists and is a mount point.

Seems like the easiest method is to include

ConditionPathIsMountPoint=/mnt/backup

In your [Unit] section. Though this doesn't check which device is mounted there, though in the case of creating a backup the user may not be picky.

Systemd Unit Start Conditions

With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

ConditionPathIsMountPoint= is similar to ConditionPathExists= but verifies whether a certain path exists and is a mount point.

Seems like the easiest method is to include

ConditionPathIsMountPoint=/mnt/backup

In your [Unit] section. Though this doesn't check which device is mounted there, though in the case of creating a backup the user may not be picky.

You'd probably also need Restart=on-failure to continue trying to start the service.


A more precise method would be to write a udev rule that starts the service when a particular device is mounted.

As a reference

Start by finding your device in lsusb. Note the ID (eg 0a81:0101)

Create a new udev rules file in /etc/udev/rules.d/ via sudoedit /etc/udev/rules.d/100-mount-videos.rulesand plonk a new rule in there like this:

ACTION=="add", ATTRS{idVendor}=="0a81", ATTRS{idProduct}=="0101", RUN+="/home/your_username/bin/mount_videos.sh"

You could just replace the RUN+= with RUN+=/usr/bin/systemctl start backup.service or whatever you called your service

Source Link
Centimane
  • 4.5k
  • 3
  • 25
  • 47

Systemd Unit Start Conditions

With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

ConditionPathIsMountPoint= is similar to ConditionPathExists= but verifies whether a certain path exists and is a mount point.

Seems like the easiest method is to include

ConditionPathIsMountPoint=/mnt/backup

In your [Unit] section. Though this doesn't check which device is mounted there, though in the case of creating a backup the user may not be picky.