Modern Linux systems use systemd to manage daemons and system settings. Systemd is a service manager and initialization system, which took over from SysvInit and has been around for nearly a decade now.
It performs various tasks such as managing daemons (starting, stopping, disabling, and enabling services), snapshot support, and process tracking among others. Currently, it is the default initialization system on most Linux distributions.
You can easily create a custom script that can run as a systemd service. In this guide, we will look at how you can run a script in a systemd just before the system shuts down on a Linux system.
For demonstration purposes, we are using the Rocky Linux 8 server.
Step 1: Create a Sample Script
The first step is to create a shell script that will be executed before shutdown. For demonstration purposes, we will create a simple shell script called sample_script.sh
that delays the shutdown of the system by 30 seconds.
To run the script right before the system powers off, you need to place the script in the /usr/lib/systemd/system-shutdown
directory. Before executing the actual shutdown, all the binary executables in the /usr/lib/systemd/system-shutdown
directory will be executed in parallel.
The execution of the action is not continued until all executable files are done.
$ sudo vim /usr/lib/systemd/system-shutdown/sample_script.sh
The simple shell script that delays the system from shutting down by 30 seconds looks as shown below.
#!/bin/bash # A script that forces the system to wait for 30 seconds before shutdown sleep 30
Save and Exit the file. Next, make the script executable.
$ sudo chmod u+x /usr/lib/systemd/system-shutdown/sample_script.sh
Step 2: Create a Systemd Unit File to Run Shell Script
The next step is to create a systemd unit file to run the shell script before the system power-off. We will create the execute-before-shutdown.service
systemd service file as shown.
$ sudo vim /etc/systemd/system/execute-before-shutdown.service
Paste the following lines of code.
[Unit] Description=Execute custom script before system poweroff DefaultDependencies=no Before=shutdown.target [Service] Type=oneshot ExecStart=/usr/lib/systemd/system-shutdown/sample_script.sh TimeoutStartSec=0 [Install] WantedBy=shutdown.target
The crucial task here is done by the Before=shutdown.target
and TimeoutStartSec=0
directives.
Save the changes and Exit the file. Thereafter, refresh the systemd configuration file and enable the script to automatically start the next time the system is powered on.
$ sudo systemctl daemon-reload $ sudo systemctl enable execute-before-shutdown.service --now
Step 3: Confirm Systemd Unit File
To confirm that the script is running before the system powers off, simply run the following shutdown command as shown:
$ sudo poweroff
You’ll observe that the shutdown process will be delayed by 30 seconds before the system eventually goes off.
Conclusion
We hope that the steps outlined in this guide have given you a bearing on how you can go about running a shell script using a systemd on a Linux system.
For some reason, my server shuts down immediately after entering the poweroff command. It doesn’t wait for 15 seconds.