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.
Generally the PID file is handled by the init script itself. If it is the python script that cleans it up, you should include that code as well...
Are you sure it is the old pid file, and not a newly created one and that the daemon is just crashing at boot? /tmp is recommended to be cleared during a boot according to the Filesystem Hierarchy Standard, see this section of that document -- not sure if this happens in CentOS or not, I thought it did though.
Update: tmpwatch is called by cron (in daily) and cleans up /tmp periodically based on atime (default), so really you should be putting them in /var/run, or they might be deleted out from under you.
So I would start by moving the pid file to /var/run and put the job of cleaning it up in the init script, and go from there.
Probably you have the service starting properly but not being stopped properly.
Review http://www.serverdensity.com/docs/agent/linuxstartup/ and ensure you have K script(s) as well as S scripts; you may have to run the following (copied from the above page):