[SOLVED] Any ideas on how I can ensure that a service is stopped before RHEL is shutdown?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Any ideas on how I can ensure that a service is stopped before RHEL is shutdown?
Hi everyone,
I have a program that I don't think its being shutdown properly prior to RHEL being shutdown (with the shutdown -h now command).
I looked around and there is no log for the shutdown process, I looked in /var/logs/messages and see nothing.
Is there a work around to add a script to issue 'service <program> stop' when 'shutdown -h now' is executed?
Is there a log that shows all the shutdown messages to proves that the program is shutdown completely?
Generally, services running on the system are listed in the /etc/init.d folder have a link in the runlevel folders as well. If your system runs in the graphical interface (runlevel 5) then your services are started at bootup in /etc/rc.d/rc5.d/, if it's command-line only check rc3.d. In those folders, you'll see symbolic links to the init.d folder's services that are named with S and K. When you enter that runlevel (i.e. boot), the K services are killed and the S services are started in the order that they're numbered. Someone feel free to correct me if I'm not saying that quite right.
My recommendation is that you make sure that you have a K90MyProgram link in the rcx.d folders for your service. (Note: substitute a good number for the 90, that puts it in a reasonable place in your kill sequence, and of course change MyProgram to the name of the service that you're trying to shutdown with the system.)
I believe that the shutdown command is essentially calling init 0
Maybe that's not the direction your question was going...
If so, look in /etc/init.d for the startup program. Check the start and stop values they'll look like this.
# chkconfig: 2345 55 25
Note that it is and should be commented out. The fist number is the runlevel(s) it will run under, the second is the start value, third, stop value.
The higher the value the later it starts & stops. 2345 10 90 will run under runlevels 2, 3, 4 and 5. It will start very early in the boot process and stop very late.
If it's not a service, you can copy one of the files under /etc/init.d/ that closely matches your needs.
When you've got it, run
chkconfig --add <program>
Use this advice at your own risk. Strange things can happen if improperly executed.
If I execute 'service tsm01 stop' it works exactly like it should. Unfortunately, when I execute a 'shutdown -h', RHEL does not stop it clean, i.e. the way 'service tsm01 stop' would. Right now the work around is to having to issue the service stop command manually before a shutdown to keep the integrity of the program. This really sucks.
Here is the tsm01 script in init.d... I would change it in a heartbeat, but I'm not at all sure what it is to change. I would check the shutdown list, but I don't know where one is?
Code:
stop() {
echo "Stopping $prog instance $instance ..."
if [[ -e $pidfile ]]
then
# make sure someone else didn't kill us already
progpid=`cat $pidfile`
running=`ps -ef | grep $prog | grep -w $progpid | grep -v grep`
if [[ -n $running ]]
then
#echo "executing cmd kill `cat $pidfile`"
kill `cat $pidfile`
total_slept=0
while check_running; do \
echo "$prog instance $instance still running, will check after $SLEEP_INTERVAL seconds"
sleep $SLEEP_INTERVAL
total_slept=`expr $total_slept + 1`
if [ "$total_slept" -gt "$MAX_SLEEP_TIME" ]; then \
break
fi
done
if check_running
then
echo "Unable to stop $prog instance $instance"
exit 1
else
echo "$prog instance $instance stopped Successfully"
fi
fi
# remove the pid file so that we don't try to kill same pid again
rm $pidfile
if [[ $? != 0 ]]
then
echo "Process $prog instance $instance stopped, but unable to remove $pidfile"
echo "Be sure to remove $pidfile."
exit 1
fi
else
echo "$prog instance $instance is not running."
fi
rc=$?
echo
[ $rc -eq 0 ] && rm -f /var/lock/subsys/dsmserv
return $rc
}
Sorry guys... I did find the K's... they are in rc0.d rc1.d rc6.d
Code:
K10tsm01
So, whatever runlevel you specify for the program, there will be a S<StartPrioritynumber><Programname>. Whatever runlevel you did not specify, that runlevel will contain K<StopPrioritynumber><Programname>.
A running service must have a file in /var/lock/subsys, that matches the service name, to be cleanly stopped via the service function as part of the shutdown process. The program creates a file named “/var/lock/subsys/dsmserv”. This matches the task name (from a “ps –e” command) but not the “service” name of “tsm01”
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.