Run script on shutdown and sleep - not working for sleep

I am trying to run a backup script when shutting down my laptop or putting it to sleep, but not when rebooting.

I am using the following backup.service file:

[Unit]
Description=Execute backup script on shutdown
After=network.target
After=local-fs.target
After=mosquitto.service
#Requires=network.target
#Requires=local-fs.target
#Wants=mosquitto.service
RequiresMountsFor=/home

[Service]
TTYPath=/dev/tty1
StandardOutput=journal+console
StandardError=journal+console
Type=oneshot
ExecStart=/bin/true
ExecStop=/etc/scripts.d/backup_duplicacy.sh
TimeoutStopSec=3600
RemainAfterExit=yes

[Install]
WantedBy=hibernate.target suspend.target hybrid-sleep.target sleep.target systemd-suspend.service

It is working fine when shutting down (using poweroff.target in otherwise similar service), but not when sleeping. When sleeping, the script does not start.

Does anybody have any idea why?

Alternatively, adding the backup script to /usr/lib/systemd/system-sleep does get it executed, but it fails because the network etc is already gone by then.

Welcome to the Forum :+1:


Check this page: systemd.special which has a list and explanation of special targets you can/should use in service units…
Points of interest to your issue:

  1. #network-online.target
  2. #remote-fs.target
  3. #sleep.target
    1. #hibernate.target
    2. #hybrid-sleep.target
    3. #suspend-then-hibernate.target
  4. #nss-lookup.target
  5. #time-sync.target

:vulcan_salute:

Yes, I already checked that page. I think I already added the relevant targets It seems to work fine for shutdown and the concept should be the same for the sleep as far as I understand systemd, just using the sleep targets instead of the poweroff target. But nope…

If you get it to work for one but not the other without editing your service unit, then why don’t make two, one for each scenario? :wink:

That means it doesn’t have the proper targets as dependencies/requirements…

PS:
A nice read which might help you too with this, although related it’s not exactly same as what you want i think.


These units used by the nVidia driver might also shed a light:

$ systemctl cat nvidia-suspend.service nvidia-resume.service nvidia-hibernate.service

# /lib/systemd/system/nvidia-suspend.service
[Unit]
Description=NVIDIA system suspend actions
Before=systemd-suspend.service

[Service]
Type=oneshot
ExecStart=/usr/bin/logger -t suspend -s "nvidia-suspend.service"
ExecStart=/usr/bin/nvidia-sleep.sh "suspend"

[Install]
WantedBy=systemd-suspend.service

# /lib/systemd/system/nvidia-resume.service
[Unit]
Description=NVIDIA system resume actions
After=systemd-suspend.service
After=systemd-hibernate.service

[Service]
Type=oneshot
ExecStart=/usr/bin/logger -t suspend -s "nvidia-resume.service"
ExecStart=/usr/bin/nvidia-sleep.sh "resume"

[Install]
WantedBy=systemd-suspend.service
WantedBy=systemd-hibernate.service

# /lib/systemd/system/nvidia-hibernate.service
[Unit]
Description=NVIDIA system hibernate actions
Before=systemd-hibernate.service

[Service]
Type=oneshot
ExecStart=/usr/bin/logger -t hibernate -s "nvidia-hibernate.service"
ExecStart=/usr/bin/nvidia-sleep.sh "hibernate"

[Install]
WantedBy=systemd-hibernate.service