Why my stop script is not being executed?
I recently got a question on why his Linux script is not executed when the system is rebooted or shutdown.
The script runs fine when run manually.
# service cleanscript stop
Cleanscript: Cleaning up the directory!
My quick response was maybe your script is not configured correctly for systemd. Upon further inspection the
server is running a Redhat 6 update 10 OS. This was still using the old init scripts.
Here is the script in question.
#!/bin/sh
#
stop() {
echo -n $"Cleanscript: Cleaning up the directory!"
rm -fr /var/tmp/*
}
case "$1" in
stop)
stop
;;
esac
I can see the problem that the script have no start function. He came back as to why does it need a start function, when the script is only needed when the server is being shutdown?
If you look at the script /etc/rc.d/rc that executes all the stop scripts, you have these lines.
It looks for a lock file, if it can't find the lock file it just ignore the script.
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
# Check if the subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K??}
[ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue
check_runlevel "$i" || continue
Here is the corrected script.
#!/bin/sh
#
start() {
echo "Cleanscript: creating the lock file"
touch /var/lock/subsys/cleanscript
}
stop() {
echo "Cleanscript: Cleaning up the directory"
rm -fr /var/tmp/*
rm -f /var/lock/subsys/cleanscript
}
case "$1" in
stop)
stop
;;
start)
start
;;
esac
Now, add the startup for cleanscript in runlevel 3.
#ln -s /etc/init.d/cleanscript /etc/rc3.d/S99cleanscript
How do you do this with systemd?
Starting with RHEL 7 it comes with a more flexible systemd for managing your services.
You will need to create these two files. The actual clean up script and the service definition.
# pwd
/etc/systemd/system
# ls -l cleanscript*
-rw-r--r--. 1 root root 211 Aug 21 21:34 cleanscript.service
-rwxr-xr-x. 1 root root 79 Aug 21 21:33 cleanscript.sh
# cat cleanscript.service
[Unit]
Description=Cleanscript: Clean up the /var/tmp directory
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/etc/systemd/system/cleanscript.sh
[Install]
WantedBy=multi-user.target
# cat cleanscript.sh
#!/bin/sh
echo -n $"Cleanscript: Cleaning up the directory!"
rm -fr /var/tmp/*
# systemctl enable cleanscript
Created symlink from /etc/systemd/system/multi-user.target.wants/cleanscript.service to /etc/systemd/system/cleanscript.service.
# systemctl status cleanscript
â cleanscript.service - Cleanscript: Clean up the /var/tmp directory
Loaded: loaded (/etc/systemd/system/cleanscript.service; enabled; vendor preset: disabled)
Active: inactive (dead)
# systemctl start cleanscript
# systemctl status cleanscript
cleanscript.service - Cleanscript: Clean up the /var/tmp directory
Loaded: loaded (/etc/systemd/system/cleanscript.service; enabled; vendor preset: disabled)
Active: active (exited) since Tue 2018-08-21 21:42:00 PDT; 2s ago
Process: 1330 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 1330 (code=exited, status=0/SUCCESS)
Aug 21 21:42:00 workstation1 systemd[1]: Starting Cleanscript: Clean up the /var/tmp directory...
Aug 21 21:42:00 workstation1 systemd[1]: Started Cleanscript: Clean up the /var/tmp directory.
Recent comments