I have an init.d script to start a Python script:
#!/bin/sh
#
###############################################################################
# sd-agent
#
# Written by Boxed Ice <[email protected]>
# A server monitoring daemon for www.serverdensity.com
#
# Licensed under Simplified BSD License (see LICENSE)
#
###############################################################################
#
# chkconfig: 345 85 15
# description: Server Density Monitoring Agent
AGENTPATH="/usr/bin/sd-agent/agent.py"
[ -f $AGENTPATH ] || echo "/usr/bin/sd-agent not found"
# Source function library.
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
fi
if [ -f /etc/SuSE-release ]; then
. /etc/rc.status
rc_reset
fi
# Action to take
case "$1" in
start)
python $AGENTPATH start
if [ -f /etc/SuSE-release ]; then
rc_status -v
elif [ -f /etc/debian_version ] || [ -f /etc/lsb-release ] || [ -f /etc/gentoo-release ]; then
echo " Started"
else
success
echo
fi
echo
;;
stop)
python $AGENTPATH stop
if [ -f /etc/SuSE-release ]; then
rc_status -v
elif [ -f /etc/debian_version ] || [ -f /etc/lsb-release ] || [ -f /etc/gentoo-release ]; then
echo " Stopped"
else
success
echo
fi
echo
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: /etc/init.d/sd-agent start|stop|restart"
exit 1
esac
exit 0
This has been "installed" into chkconfig:
[root@test ~]# chkconfig --list sd-agent
sd-agent 0:off 1:off 2:off 3:on 4:on 5:on 6:off
If I execute:
service sd-agent start
Then the script runs as expected. The Python code creates a PID file at /tmp/sd-agent.pid as it is supposed to. Equally, if I execute
service sd-agent stop
then the script is terminated and the PID file is removed.
If I stop the script and then reboot the server, it is started when the server finished the boot cycle. This is expected because I have set it to do that with chkconfig.
However, if I start the script, then reboot the server, the stop command does not seem to execute because when the server comes back up, the old /tmp/sd-agent.pid file still exists. This is preventing the start command from executing because it checks for the existence of the PID file and will not start if one already exists.
It seems that the stop command is not being executed when I issue the reboot command even though calling it directly works fine.
Any suggestions as to why?
This is on CentOS 5.2.