I have been running a script on shutdown for many years, which backs up my files to an external disk. Nothing flash, just a series of rsyncs. The last incarnation of my box, was running Fedora 20 and when I installed it, rather than fathoming the mysteries of systemd, I just added my backup script as a sys5init script and it all just worked - a successful backup each shutdown.
My box then died due to a fried motherboard and, for reasons than I won’t go into, I am running openSuse 13.2 on the new machine. I put my backup script in as before but it appears as though openSuse does not process sys5init scripts. Well my backup did not work. So I created a systemd script to call my backup script and it runs successfully on shutdown. Hooray! Unfortunately, systemd does not wait for my script to complete before shutting the system down, so my backup is incomplete. Boo!
How do I tell systemd to wait until my script has finished? Below is my systemd service script. I hoped that the “RequiresMountsFor”, which contains a full list of mounted filesystems that I want to backup, would cause it to wait.
[Unit]
Description=backup my stuff to usb
Before=shutdown.target halt.target
RequiresMountsFor=/data1 /home /usr/local /spare1 /spare2
On Wed 23 Sep 2015 07:26:01 PM CDT, MMonroe1 wrote:
I have been running a script on shutdown for many years, which backs up
my files to an external disk. Nothing flash, just a series of rsyncs.
The last incarnation of my box, was running Fedora 20 and when I
installed it, rather than fathoming the mysteries of systemd, I just
added my backup script as a sys5init script and it all just worked - a
successful backup each shutdown.
My box then died due to a fried motherboard and, for reasons than I
won’t go into, I am running openSuse 13.2 on the new machine. I put my
backup script in as before but it appears as though openSuse does not
process sys5init scripts. Well my backup did not work. So I created a
systemd script to call my backup script and it runs successfully on
shutdown. Hooray! Unfortunately, systemd does not wait for my script to
complete before shutting the system down, so my backup is incomplete.
Boo!
How do I tell systemd to wait until my script has finished? Below is my
systemd service script. I hoped that the “RequiresMountsFor”, which
contains a full list of mounted filesystems that I want to backup, would
cause it to wait.
[Unit]
Description=backup my stuff to usb
Before=shutdown.target halt.target
RequiresMountsFor=/data1 /home /usr/local /spare1 /spare2
[Unit]
Description=backup my stuff to usb
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
RequiresMountsFor=/data1 /home /usr/local /spare1 /spare2
[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/local/bin/usbdisk_backup
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
Else, create a systemd service file that points at your old init
script, eg;
[Service]
ExecStop=/etc/init.d/mybackup start
–
Cheers Malcolm °¿° LFCS, SUSE Knowledge Partner (Linux Counter #276890)
SUSE Linux Enterprise Desktop 12 | GNOME 3.10.1 | 3.12.44-52.10-default
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below… Thanks!
Unfortunately, systemd does not wait for my script to complete before shutting the system down, so my backup is incomplete.
It is not clear, what happens - systemd does not wait for ExecStop at all or it waits but hits timeout. You need to be more specific. In the latter case setting TimeoutStopSec may help.
Before=shutdown.target halt.target
That is pretty much useless. Those dependencies apply to unit start only, and normal services are never started together with either shutdown.target or halt.target.
ExecStop=/usr/local/bin/usbdisk_backup
Does this command run synchronously? How long does it take?