Systemd not running a script in system-sleep folder

I’ve placed a script in /usr/lib/systemd/system-sleep and used chmod to make it executable.

[me@Laptop system-sleep]$ ls -l outlet
-rwxr-xr-x 1 root root 206 Jun  8 21:33 outlet

The script runs fine if I execute it manually, but it does not run when I suspend the laptop.

Here’s the script, which just uses IFTTT to shut off the smart outlet my laptop is plugged into:

echo "we are testing at $(date)..." > /tmp/test
echo $1 >> /tmp/test
if [ "${1}" == "pre" ]; then    
        curl -X POST

What do I need to do to get it to run when I suspend my laptop? I have also placed the script in the shutdown folder, and it doesn’t run there either when I shut down the laptop.

Hi @sleepyhead, and welcome!

While I do not have a laptop, or even use suspend, I did a quick search and according to this page:

Running A Command Before Suspend

To run a script before suspend, place any bash script file in the /usr/lib/systemd/system-sleep/ directory. The scripts themselves can do anything, but there is a certain syntax that must be adhered to. Start by opening up a terminal window. Using sudo -s, gain a root shell.

Next, up the nano text editor inside the system-sleep directory:

nano /usr/lib/systemd/system-sleep/

Add the shebang to the start of the script. Adding this is critical, and without it, the interpreter will not be able to correctly read the code and commands.


The next part of the script is the if. This will tell the power control system that if the system is about to go down for suspend, something should happen. Paste the code below:

if [ "${1}" == "pre" ]; then sh /path/to/script/to/

Change sh /path/to/script/to/ in the script to whatever you’d like to run on your Linux PC (or server) right before the system goes down for suspend. Keep in mind that the line directly after if does not need to be sh /path/to/script/to/ You can also use this to execute various commands. As long as bash can recognize it as a command, it will run.

The last part of the “before suspend” script is to add the “else if” portion. This aspect of the script doesn’t need to be modified, as, in this example, we’re worried about doing something before the system goes down, and not when it wakes back up.

elif [ "${1}" == "post" ]; then # nothing goes here fi

When everything has been added to the file, press Ctrl+O to save nano.

Running A Command After Resume

Executing a command after resume works much like running something before suspend. The only real difference is instead of adding a command after the if portion of the script, you’d make the most important part of the code occur after the elif line.

To make a post-resume script, first, add the shebang as the first line.


On the next line, add the if portion of the script.

if [ "${1}" == "pre" ]; then # nothing goes here

With that bit of code pasted into the bash script, move down and paste the “else if” section.

elif [ "${1}" == "post" ]; then sh /path/to/script/to/ fi

Once again, it’s OK to write anything under the elif line. As long as it is a normal command operation systemd and bash will run it.

Hope this helps!

1 Like

Thanks Mirdathos. These steps are pretty much what I’ve done. I altered a couple of minor things to match the article you found, (added the .sh extension, added the elif clause) but the script still doesn’t run when I suspend.

Well, them I’m officially out of ideas, and don’t know how to proceed. Sorry.

Well, as it turns out, the script is running, as can be seen with the command:

journalctl -b -u systemd-suspend.service
Jun 09 10:36:19 HP-Laptop systemd[1]: Starting Suspend...
Jun 09 10:36:19 HP-Laptop systemd-sleep[76628]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Jun 09 10:36:19 HP-Laptop systemd-sleep[76628]:                                  Dload  Upload   Total   Spent    Left  Speed
Jun 09 10:36:19 HP-Laptop systemd-sleep[76628]: [128B blob data]
Jun 09 10:36:19 HP-Laptop [76624]: /usr/lib/systemd/system-sleep/ failed with exit status 6.
Jun 09 10:36:19 HP-Laptop systemd-sleep[76620]: Suspending system...

But, it’s just not actually doing anything. I’m guessing maybe the network is shutdown before the script runs, so it can’t connect to the IFTTT service. Not sure. I haven’t been able to find any details yet about what “exit status 6” means.

Thanks again for your help!

That is an error code.

What it means here I have no idea.

Yep, I found that same article. So it seems the “exit status 6” is from bash, not from systemd.

[me@Laptop system]$ perror 6
OS error code   6:  No such device or address

So maybe the “address” is referring to the URL of the https call that isn’t reachable because maybe the networking is already shut down.

That would seem exactly it, yes.

If I disable networking, and run the script manually:

[me@Laptop system-sleep]$ ./ pre
curl: (6) Could not resolve host:

So that (6) seems to confirm our suspicions. Not sure where to go from here. I might have to resort to manually turning off the outlet. The horror!

Like a savage!