Running scripts before and after suspend with systemd

I’ve had this question a few times, so it’s probably a good candidate for my blog.

If you want to do something before you suspend, like unload a module or run some script, it’s quite easy with systemd. Similarly, you can easily do something when the system resumes (like reload the module).

The details are in the systemd-suspend man page:
man systemd-suspend.service

Simply put an executable script of any name under /usr/lib/systemd/system-sleep/ that checks whether the first argument is pre (for before the system suspends) or post (after the system wakes from suspend).

If it is pre, then do the thing you want to before suspend, if it’s post then do the thing you want to do after resume. Simple!

Here’s a useless example:
#!/bin/sh
if [ "${1}" == "pre" ]; then
  # Do the thing you want before suspend here, e.g.:
  echo "we are suspending at $(date)..." > /tmp/systemd_suspend_test
elif [ "${1}" == "post" ]; then
  # Do the thing you want after resume here, e.g.:
  echo "...and we are back from $(date)" >> /tmp/systemd_suspend_test
fi

10 Responses to “Running scripts before and after suspend with systemd”


  • Other users of Debian might want to take notice that the path to place the files under for that distribution is /lib/systemd/system-sleep/, i.e. without the initial /usr. Although this actually is properly documented with the correct path in the man page, it is a detail which is easy to miss.

  • Thanks for mentioning this.

  • Supersonic Tumbleweed

    Thank you, this helped me a lot!
    I actually expected lots of hacking but it’s just a systemd thing.
    Still, saved me a lot of manpage digging!
    Worked on Fedora with systemd!

  • Thanks, now my keyboard backlight settings get saved and restored correctly on suspend/resume 🙂

  • I wanted to make a backup to my server before suspend, but find that the network is shut down before my “pre-script” is executed.

    A solution appears quite complicated:
    https://unix.stackexchange.com/questions/337853/how-can-i-trigger-a-systemd-unit-on-suspend-before-networking-is-shut-down

    Any other solution?

  • Hi Klaus,

    I haven’t tested that, but it looks like by the time you get to running the script as per my blog, the suspend process has already begun which has taken out your networking service.

    The solution looks complicated, but basically it’s just a systemd service file which does something just before suspend. For you, that could be the server backup. Keep in mind though that even if you did this, there are timeouts you’ll need to contend with. Also, it seems to me that you won’t get any visibility into the backup (did it really finish before your system was suspended?).

    Is there a reason you have to do your backup before suspend? Can you just run backups on an hourly cronjob or something? If you’re using rsync (or rsnapshot) then you’ll just send the differences anyway.

    -c

  • Hi Chris,

    Thanks for your answer and thanks for the good work.

    You are right that crontab is a way to go, though I would prefer to back-up automatically, when I am finished working and closing down.
    (combined with ad-hoc backups).

    With respect to timeouts etc. my plan was to make a check at restart.

    Yes, I am using rsync and it is working perfect, but since it is often remotely (ssh+internet) it can be slow and time consuming and annoying while doing other work.(Thunderbird is a bad choice in that respect, since even small changes can cause large files to be sync’ed).

  • Hi,

    Thanks for the example code!

    I’ve just started using Linux and I’ve been pulling my hair over my tuner card crashing after suspend, but now I’m finally able to unload Tvheadend and my TV tuner driver before suspend and restart them after suspend.

    I had to make a small adjustment to the script, since I got an error “unexpected operator” in the if-statements.

    Before if [ “${1}” == “pre” ]; then
    After if [ “${1}” = “pre” ]; then

    So, one “equal” sign less.
    I’m running Linux Mint 19.1, kernel 4.18.0-13
    I hope this is helpful for someone.

  • OK great. You might have a different shell to me, but anyway if it works, great.

Leave a Reply