How to run a script before Systemd shutdown in LInux

by time news

2023-07-18 08:30:00

Modern Linux systems use systemd to manage daemons and system configuration. Systemd is a service manager and initialization system, which replaced SysvInit and has been around for almost a decade.

It performs various tasks such as daemon management (starting, stopping, disabling, and enabling services), snapshot support, and process tracking, among others. Currently, it is the default boot system in most Linux distributions.

You can easily create a script custom that can be run as a service systemd . In this guide, we will see how you can run a script on a systemd just before the system shuts down on a system Linux .

For demo purposes we are using the server Rocky Linux 8. But it would work just as well on RHEL, Fedora, AlmaLinux, etc.

Step 1: Create a Sample Script

The first step is to create a script de shell which will be executed before shutdown. For demonstration purposes, we will create a simple shell script called “sample_script.sh” that delays system shutdown in 30 seconds .

To run the script just before the system shuts down, you must place the script in the “/usr/lib/systemd/system-shutdown” directory. Before executing the actual shutdown, all binary executables in the “usr/lib/systemd/system-shutdown” directory will be executed in parallel.

Action execution does not continue until all executable files are complete.

$ sudo vim /usr/lib/systemd/system-shutdown/sample_script.sh

He script de shell simple that delays system shutdown for 30 seconds looks like below.

#!/bin/bash # A script that forces the system to wait 30 seconds before shutting down sleep 30

keep y out from 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 the shell script

The next step is to create a systemd unit file to run the script de shell before the system shuts down. We will create the “execute-before-shutdown.service” systemd service file as shown.

$ sudo vim /etc/systemd/system/execute-before-shutdown.service

Take it 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 performed by the “Before=shutdown.target” and “TimeoutStartSec=0” directives.

keep the changes and out from the file. Thereafter, update the configuration file the systemd and enable the script to start automatically the next time the system is powered on.

$ sudo systemctl daemon-reload
$ sudo systemctl enable execute-before-shutdown.service –now

Step 3: Confirm the Systemd drive file

To confirm that the script is running before the system shuts down, simply run the following command power off as shown:

$ sudo poweroff

You will notice that the shutdown process will be delayed 30 seconds before the system finally shuts down.

Conclusion

We hope the steps outlined in this guide have given you an idea of ​​how you can run a shell script using systemd. in a Linux system.

#run #script #Systemd #shutdown #LInux

You may also like

Leave a Comment