How to start a systemd service after mount command

Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I am working on a lab for my linux class and the teacher is teaching us about Systemd.
How do I create a service that starts a script when a specific device is mounted using this command: mount /dev/xvdc1 /mnt/backup
What I have tried
- Using a Timer I checked if
/mnt/backupwas a mount point. (teacher would not accept it. It must be strictly event driven) - Creating a .mount unit and making the .service unit require the .mount unit. This approach did not start the script when executing the command:
mount /dev/xvdc1 /mnt/backup
I think I may be approaching this problem the wrong way. This is the lab problem.
"Create a unit file that copies all the files from
/mnt/backup
using rsync as soon as/dev/xvdc1is mounted."
What I know:
- The device that will be mounted will always be
/dev/xvdc1 - The mount point of the device will always be
/mnt/backup - The Service must be started based on
/dev/xvdc1being mounted
Here is my .service unit file
[Unit]
Description=Starts a backup for /dev/xvdc1 when mounted using the mount command.
[Service]
Type=simple
ExecStart=/root/backupscript.sh
ExecStop=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
centos systemd services
add a comment |Â
up vote
5
down vote
favorite
I am working on a lab for my linux class and the teacher is teaching us about Systemd.
How do I create a service that starts a script when a specific device is mounted using this command: mount /dev/xvdc1 /mnt/backup
What I have tried
- Using a Timer I checked if
/mnt/backupwas a mount point. (teacher would not accept it. It must be strictly event driven) - Creating a .mount unit and making the .service unit require the .mount unit. This approach did not start the script when executing the command:
mount /dev/xvdc1 /mnt/backup
I think I may be approaching this problem the wrong way. This is the lab problem.
"Create a unit file that copies all the files from
/mnt/backup
using rsync as soon as/dev/xvdc1is mounted."
What I know:
- The device that will be mounted will always be
/dev/xvdc1 - The mount point of the device will always be
/mnt/backup - The Service must be started based on
/dev/xvdc1being mounted
Here is my .service unit file
[Unit]
Description=Starts a backup for /dev/xvdc1 when mounted using the mount command.
[Service]
Type=simple
ExecStart=/root/backupscript.sh
ExecStop=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
centos systemd services
I found this question <unix.stackexchange.com/questions/91443/â¦; but it seems like I will have to create a .mount unit instead
â Loligans
Jan 14 '17 at 23:39
Try it and if it works, you can answer your own question. ;)
â Julie Pelletier
Jan 15 '17 at 4:22
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I am working on a lab for my linux class and the teacher is teaching us about Systemd.
How do I create a service that starts a script when a specific device is mounted using this command: mount /dev/xvdc1 /mnt/backup
What I have tried
- Using a Timer I checked if
/mnt/backupwas a mount point. (teacher would not accept it. It must be strictly event driven) - Creating a .mount unit and making the .service unit require the .mount unit. This approach did not start the script when executing the command:
mount /dev/xvdc1 /mnt/backup
I think I may be approaching this problem the wrong way. This is the lab problem.
"Create a unit file that copies all the files from
/mnt/backup
using rsync as soon as/dev/xvdc1is mounted."
What I know:
- The device that will be mounted will always be
/dev/xvdc1 - The mount point of the device will always be
/mnt/backup - The Service must be started based on
/dev/xvdc1being mounted
Here is my .service unit file
[Unit]
Description=Starts a backup for /dev/xvdc1 when mounted using the mount command.
[Service]
Type=simple
ExecStart=/root/backupscript.sh
ExecStop=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
centos systemd services
I am working on a lab for my linux class and the teacher is teaching us about Systemd.
How do I create a service that starts a script when a specific device is mounted using this command: mount /dev/xvdc1 /mnt/backup
What I have tried
- Using a Timer I checked if
/mnt/backupwas a mount point. (teacher would not accept it. It must be strictly event driven) - Creating a .mount unit and making the .service unit require the .mount unit. This approach did not start the script when executing the command:
mount /dev/xvdc1 /mnt/backup
I think I may be approaching this problem the wrong way. This is the lab problem.
"Create a unit file that copies all the files from
/mnt/backup
using rsync as soon as/dev/xvdc1is mounted."
What I know:
- The device that will be mounted will always be
/dev/xvdc1 - The mount point of the device will always be
/mnt/backup - The Service must be started based on
/dev/xvdc1being mounted
Here is my .service unit file
[Unit]
Description=Starts a backup for /dev/xvdc1 when mounted using the mount command.
[Service]
Type=simple
ExecStart=/root/backupscript.sh
ExecStop=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
centos systemd services
centos systemd services
edited Jan 14 '17 at 21:05
asked Jan 14 '17 at 5:12
Loligans
264
264
I found this question <unix.stackexchange.com/questions/91443/â¦; but it seems like I will have to create a .mount unit instead
â Loligans
Jan 14 '17 at 23:39
Try it and if it works, you can answer your own question. ;)
â Julie Pelletier
Jan 15 '17 at 4:22
add a comment |Â
I found this question <unix.stackexchange.com/questions/91443/â¦; but it seems like I will have to create a .mount unit instead
â Loligans
Jan 14 '17 at 23:39
Try it and if it works, you can answer your own question. ;)
â Julie Pelletier
Jan 15 '17 at 4:22
I found this question <unix.stackexchange.com/questions/91443/â¦; but it seems like I will have to create a .mount unit instead
â Loligans
Jan 14 '17 at 23:39
I found this question <unix.stackexchange.com/questions/91443/â¦; but it seems like I will have to create a .mount unit instead
â Loligans
Jan 14 '17 at 23:39
Try it and if it works, you can answer your own question. ;)
â Julie Pelletier
Jan 15 '17 at 4:22
Try it and if it works, you can answer your own question. ;)
â Julie Pelletier
Jan 15 '17 at 4:22
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
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", ATTRSidVendor=="0a81", ATTRSidProduct=="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
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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", ATTRSidVendor=="0a81", ATTRSidProduct=="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
add a comment |Â
up vote
0
down vote
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", ATTRSidVendor=="0a81", ATTRSidProduct=="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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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", ATTRSidVendor=="0a81", ATTRSidProduct=="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", ATTRSidVendor=="0a81", ATTRSidProduct=="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
edited Apr 27 '17 at 19:45
answered Apr 27 '17 at 19:38
Centimane
3,1341933
3,1341933
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f337301%2fhow-to-start-a-systemd-service-after-mount-command%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I found this question <unix.stackexchange.com/questions/91443/â¦; but it seems like I will have to create a .mount unit instead
â Loligans
Jan 14 '17 at 23:39
Try it and if it works, you can answer your own question. ;)
â Julie Pelletier
Jan 15 '17 at 4:22