- How to run a script with systemd right before shutdown?
- 4 Answers 4
- How to run script with systemd right before shutdown in Linux
- Step 1: Overview on systemd
- Step 2: Create Sample Script
- Step 3: Create unit file to run script with systemd right before shutdown
- Step 3: Verify the systemd unit file configuration
- 15 thoughts on “How to run script with systemd right before shutdown in Linux”
How to run a script with systemd right before shutdown?
What do I need to put in the [install] section, so that systemd runs /home/me/so.pl right before shutdown and also before /proc/self/net/dev gets destroyed?
[Unit] Description=Log Traffic [Service] ExecStart=/home/me/so.pl [Install] ?
4 Answers 4
The suggested solution is to run the service unit as a normal service — have a look at the [Install] section. So everything has to be thought reverse, dependencies too. Because the shutdown order is the reverse startup order. That’s why the script has to be placed in ExecStop= .
The following solution is working for me:
[Unit] Description=. [Service] Type=oneshot RemainAfterExit=true ExecStop= [Install] WantedBy=multi-user.target
RemainAfterExit=true is needed when you don’t have an ExecStart action.
After creating the file, make sure to systemctl daemon-reload and systemctl enable yourservice —now .
I just got it from systemd IRC, credits are going to mezcalero.
My assumption to WHY RemainAfterExit=true is required when there is no ExecStart is because systemd will not attempt to run ExecStop if it thinks that the service is not running. RemainAfterExit=true causes systemd to believe that the service is running, thereby causing it to run ExecStop at shutdown.
Is there anyway to ensure this script will run and complete before user sessions and user processes are terminated?
@richmb Not at all. From the docs: Note that it is usually not sufficient to specify a command for this setting that only asks the service to terminate (for example, by queuing some form of termination signal for it), but does not wait for it to do so.
@2rs2ts, can you clarify «does not work» with more specificity? Does the service not load? Does its ExecStop begin execution but get killed before the process is able to complete? Something else?
- To run a service right before starting any of reboot/shutdown/halt/kexec services (i.e. in the last moment before root filesystem becomes remounted read-only) use this service config:
[Unit] Description=Save system clock on shutdown DefaultDependencies=no After=final.target [Service] Type=oneshot ExecStart=/usr/lib/systemd/scripts/fake-hwclock.sh save [Install] WantedBy=final.target
systemctl enable my_service.service
Immediately before executing the actual system halt/poweroff/reboot/kexec systemd-shutdown will run all executables in /usr/lib/systemd/system-shutdown/ and pass one arguments to them: either «halt», «poweroff», «reboot» or «kexec», depending on the chosen action. All executables in this directory are executed in parallel, and execution of the action is not continued before all executables finished.
How to run script with systemd right before shutdown in Linux
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:
- How to execute a command or script with systemd at shutdown only and not at reboot in Linux
- How to execute a command or script with systemd right before login prompt appears on terminal in Linux
- How to execute a command or script at system startup using systemd without using cronjob in Linux
- How to execute a command or script after N minutes of boot up (system startup) with systemd in Linux
- How to halt system reboot or shutdown and read user input during boot up stage in Linux
- How to execute a command or script using systemd right before shutdown happens in Linux
- How to run a service as a specific user or group using systemd in Linux
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/startup_script.sh . 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/startup_script.sh #!/bin/bash z=0 for i in ; do sleep 1m ((z++)) echo "" done
Provide executable permission to the script
[root@centos-8 ~]# chmod u+x /tmp/startup_script.sh
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 [Unit] Description=Run my custom task at shutdown DefaultDependencies=no Before=shutdown.target [Service] Type=oneshot ExecStart=/tmp/startup_script.sh TimeoutStartSec=0 [Install] WantedBy=shutdown.target
Here the main task is done by Before=shutdown .target and TimeoutStartSec=0 . For more details check man page of systemd.service
You can also create your own man page with a list of instructions for a script or a custom tool which you have created. In real time production environment it is always recommended to also create and release a man page for every script or tool we develop.
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/shutdown.target.wants/run-before-shutdown.service → /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.
We are using Oracle VirtualBox so post reboot we will check the console of our Virtual Machine
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
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
15 thoughts on “How to run script with systemd right before shutdown in Linux”
It doesn’t work correctly.
1) Script runs at shutdown but has no access to internet, so there no way to perform a back-up to cloud storage.
2) Script also (!) starts at boot-up and during boot-up it has internet access and performs a back-up.
Ubuntu 19.10 & systemd 242 Reply
Hello Yevhenii, Thanks for your feedback.
1. If you also need active internet then you should use
Before= shutdown.target network.target
under [Unit] section
2. That should not be the case as we are explicitly defining to start before shutdown.target. Can you share me the logs from /var/log/message for further debugging Reply
It did not run for me, i am using Amazon2 AMI i did exactly what you did just change in the script, i created a shell script in which i am stopping tomcat and echoing some text in a log file. That log file is on shared location i.e. on efs, and when i try to check efs from other ec2 nodes i can not see any log file created. That means your thing did not work for me. Could you please again check this? Reply
Hi Mohit, Did you checked with local storage? Is it possible your shared storage is not accessible at the time the script was writing to log file? You should also add some commands to check the currently mounted file system at the time the script is getting called. Reply
Hey could you please help me in checking that?
Actually when i run the lifecycle hooks the same script runs and it creates a file in efs. I have 4 ec2 instances and i only terminate on and efs if shard between these 4. I could check on other ec2. But when i wanted to use your script it didnt even create.
I also edited that script to make some DB changes like updating a column value so this made me confident enough that your script didn’t update it but lifecycle hooks did. I don’t want to use lifecycle hooks that is why i read your blog and its good. But not sire why its not making any difference to my requirement 😥 Reply
Hey, sorry for the late reply.
So here is somethings i tried:
My script will shutdown tomcat, creates a empty file on a shared efs and make a DB change
1. If i use LIFECYCLE hooks the same script makes does all the three things as mentioned above.
2. If i use your solution it did not do anything.
Could you please suggest? I mean strange it did not run for me.
I am assuming that Shutdown occurs every-time we Terminate or stop EC2 instance. Reply
Hello Mohit, I am not very familiar with EC2 instances. I have verified this solution on Physical HW and Virtual Machines where it seemed to work as expected. In your case first we need to understand whether the script gets called in the first place
You can add systemctl list-jobs >> /tmp/file to your script to check list of active jobs
Also modify Before=shutdown.target poweroff.target halt.target
It can be the way the node goes down. Try a normal shutdown and observe the behaviour.
I don’t have any such setup or else I would have tried to check this locally so I am afraid I can only give hints. Reply
[Unit]
Description=Stop CDH cluster before shutdown
DefaultDependencies=no
Before=shutdown.target network.target poweroff.target halt.target [Service]
Type=oneshot
ExecStart=/usr/bin/python2.7 /usr/bin/clusterstop.py
TimeoutStartSec=0 [Install]
WantedBy=shutdown.target I set my halt time service like this. It is not getting triggered at all the the time of Shutdown or Reboot. Works while invoking the service manually.
What can be the configuration issue here? On reboot I am seeing python error messages under service status, I am not sure python is getting env at the time of service invoke. Reply
Hi Jinesh, I believe your script is getting called, just that the execution fails which is why you are seeing python error messages. If it was environment variable problem then your script wouldn’t get called in the first place.
There may be some dependency for the script? Does it require file system to be accessible or network as in such case we have to modify the systemd unit file accordingly. In my case here the script is running for sure but in the background other services may be going down in parallel so we need add dependency Reply
This scrips works in debian 11, but only executes at powerdown and not reboot. You could change as follows:
[Unit] Description=Run my custom task at shutdown DefaultDependencies=no Before=shutdown.target reboot.target [Service] Type=oneshot ExecStart=/tmp/startup_script.sh TimeoutStartSec=0 [Install] WantedBy=shutdown.target reboot.target
I wrote a new custom script to take down the shutdown time, but the service was never triggerred. I am wondering if the storage part had been umounted at that stage. Reply
You can modify your script to check the current status of connected disks when the script is getting executed. In systemd everything happens in parallel unless we have defined explicit dependency so this can become tricky sometimes to debug. Reply
Been banging my head against the wall for about six hours now trying to get a zpool export service to run at shutdown and not killed/skipped (over 300+ google searches probably). This was the only, and I mean only one that worked. Thank you so much for this 🙏 Reply