Please help me tame systemd

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

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/local/bin/usbdisk_backup
RemainAfterExit=yes

[Install]
WantedBy=local-fs.target

Thanks in advance to you clever people!

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

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/local/bin/usbdisk_backup
RemainAfterExit=yes

[Install]
WantedBy=local-fs.target

Thanks in advance to you clever people!

Hi
Try the following;


[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!

It does.

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?

Thank you very much to you both. You have solved my problem and my backup is completing. I can sleep more soundly! Thanks again.

It would be beneficial to others if you gave short summary what exactly did you do to solve your problem.

We are here to help each other and to learn from each other.

Thus please post your now correct configuration to help others after some people helped you first.

Thanks in advance :wink:

I changed my script to what malcolmlewis said to try in the second post, and it just worked. I like things that just work. :slight_smile: