Skip to main content
Bumped by Community user
Bumped by Community user
Tweeted twitter.com/StackUnix/status/821033231613104128
edited title
Link
Loligans
  • 193
  • 1
  • 1
  • 4

How to start a systemsystemd service after mount command

Source Link
Loligans
  • 193
  • 1
  • 1
  • 4

How to start a system service after mount command

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

  1. Using a Timer I checked if /mnt/backup was a mount point. (teacher would not accept it. It must be strictly event driven)
  2. 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/xvdc1 is mounted."

What I know:

  1. The device that will be mounted will always be /dev/xvdc1
  2. The mount point of the device will always be /mnt/backup
  3. The Service must be started based on /dev/xvdc1 being 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