How To Run Scripts And Commands On Suspend And Resume on Linux
The best way to run scripts and commands on suspend and resume on Linux before suspending (or after resuming) is to use the systemd init system. This is due to the fact that the init system interacts directly with the kernel, and power systems. Learning to work with this tool will allow any user to gain a lot of control over their Linux operating system,
Please understand that messing with the init system on your PC can be dangerous if you do not know what you are doing. Be sure to follow the instructions in this tutorial as closely as possible, to avoid any possible mishaps.
Not every modern Linux distribution supports the systemd init system. Some use upstart or something else entirely. In this tutorial, we’ll focus on systemd as it is the most common initiation system. If you’re looking to accomplish this task on something that doesn’t run what we cover in this article, consider looking into your operating system’s manual for instructions.
Note: Debian users should replace all instances of usr/lib/systemd/system-sleep with /lib/systemd/system-sleep/
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/pre-suspend.sh
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.
#!/bin/bash/
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/run.sh
Change sh /path/to/script/to/run.sh 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/run.sh. 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.
#!bin/bash
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/run.sh 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.
Running Commands At Startup And Resume
If you’d like to run two opposite commands; one before suspend, and one at resume, it is possible. It just means customizing the script and adding lines under both the ‘if” line, as well as the “elseif” line.
Like usual, start out by posting the shebang into the script for the interpreter.
#!/bin/bash
Next, paste the if section of the code.
if [ "${1}" == "pre" ]; then # replace with command
Under if, replace “# replace with command” with whatever command you’d like to run right before the system goes down. Then, move to the “elif” section, and do the same.
elif [ "${1}" == "post" ]; then # replace with command fi
Save the file with Ctrl + O,
Updating Permissions And Using The Script
All of the necessary commands are inside of the script file, but before it can run, you’ll need to update the permissions. Open up a terminal, and gain root with: su or sudo -s. Then do:
chmod +x /usr/lib/systemd/system-sleep/pre-suspend.sh
From now on, whenever you suspend or resume, custom commands inside of the script should run.
Note: have questions about systemd system-sleep? Check the manual with man systemd-suspend.service.
Bonjour, Hi,
Congratulations on your article, very clear, precise.
systemd is installed correctly :
systemd (245.4-4ubuntu3.18) focal;
however the directory /usr/lib/systemd/ exist
but the system-sleep/ does not!
I get a man page for systemd-suspend.service
but the system don’t know systemd-sleep
systemd-sleep –version
systemd-sleep : commande introuvable
what to do to go further?
Thanks in advance
Claude with Ubuntu Mate 20.04
Unless I’m missing something, there’s a couple of errors in this article. The bash shebang is #!/bin/bash but it’s wrong twice above. Also, bash requires a null command in an empty if loop, so # nothing goes causes an error.
Poor article. Why do you put braces around $1? Why don’t you process $2 as well? Why do you create two scripts (pre-suspend.sh and run.sh) to effect a single action? Why is the complete script (bad as it is) not made available at the bottom of the article?
At least you put a link to the man page, making this article only a partial waste of bytes.
You give the first line of code as
#!/bin/bash/
That last / shouldn’t be there.
Thanks for this guide – it’s helpful.
Hi Derrik, I enjoy reading your articles, it seems that you know just about everything of Linux…
Question: I use Mailnag in my Linux Lite setup, but when resuming from suspend it never “wakes up” I have to open the config. box, close it off and back on again to make it work. They say it’s a bug.(?)
I also heard that a nice script, as a workaround, could fix the problem. What do you think???
Istvan
Gracias 😀