How to run script with systemd right before shutdown in Linux

Written By - admin

In this article I will share example and sample systemd unit service file to run script with systemd right before shutdown in CentOS/RHEL 7/8 Linux. Using this systemd unit file you can run either some command or script as a last service before shutdown in Linux.


Some more articles on similar topic:


Now ideally any systemd service gets called via ExecStop at shutdown stage and with ExecStart at boot up stage. But here our requirement is little different, we wish to run script with systemd right before shutdown stage. Now this can be a normal shutdown or reboot.


Step 1: Overview on systemd

I hope you are already familiar with below topics


Step 2: Create Sample Script

Now to run script with systemd right before shutdown we need a script or command. For the sake of this article I will use a script from another article of mine /tmp/ This script will run for 3 minutes in a loop to check and make sure the script is not killed due to shutdown. We want the script to run and halt the shutdown until completely executed

# cat /tmp/
for i in {1..3}; do
    sleep 1m
	echo ""

Provide executable permission to the script

[root@centos-8 ~]# chmod u+x /tmp/


Step 3: Create unit file to run script with systemd right before shutdown

Now as highlighted under step 1, I have already written another article with the steps to create a new systemd unit file. Here we will name our systemd unit file as run-before-shutdown.service under /etc/systemd/system. Below is the content of run-before-login-prompt.service

[root@centos-8 ~]# cat /etc/systemd/system/run-before-shutdown.service
Description=Run my custom task at shutdown



Here the main task is done by and TimeoutStartSec=0. For more details check man page of systemd.service

TimeoutStartSec=   	When a service doesn't signal start-up completion within TimeoutStartSec, systemd considers the service failed; 
			for long-running shell scripts it is essential to modify TimeoutStartSec or disable the timeout logic altogether 
			as above, with TimeoutStartSec=0. See man systemd.service for more details.

Before=   		If the script needs to be run before other services--for example, prior to starting sshd or console/graphical 
			logins ensure there is a Before=XYZ.service in the [Unit] section and a corresponding RequiredBy=XYZ.service 
			in the [Install] section.

Refresh the systemd configuration files

[root@centos-8 ~]# systemctl daemon-reload

Enable the script to automatically start at next boot

[root@centos-8 ~]# systemctl enable run-before-shutdown.service
Created symlink /etc/systemd/system/ → /etc/systemd/system/run-before-shutdown.service.


Step 3: Verify the systemd unit file configuration

Now since we are done with the setting up of systemd. Let us verify our configuration.

[root@centos-8 ~]# reboot

We are using Oracle VirtualBox so post reboot we will check the console of our Virtual Machine
How to run script with systemd right before shutdown in Linux

As expected we see that our systemd service is running and not allowing shutdown to complete.


Lastly I hope the steps from the article to run script with systemd right before shutdown on CentOS/RHEL 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.


Related Searches: run script with systemd right before shutdown. how to execute shell script before shutdown. run script before shutdown linux. run script on shutdown linux

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!