1

I have an init.d script to start a Python script:

#!/bin/sh
#
###############################################################################
# sd-agent
#
# Written by Boxed Ice <customer.service@boxedice.com>
# 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.

davidmytton
  • 666
  • 3
  • 7
  • 17

2 Answers2

1

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):

ln -s /etc/rc.d/init.d/sd-agent /etc/rc.d/rc0.d/K15sd-agent
ln -s /etc/rc.d/init.d/sd-agent /etc/rc.d/rc1.d/K15sd-agent
ln -s /etc/rc.d/init.d/sd-agent /etc/rc.d/rc2.d/K15sd-agent
ln -s /etc/rc.d/init.d/sd-agent /etc/rc.d/rc3.d/S85sd-agent
ln -s /etc/rc.d/init.d/sd-agent /etc/rc.d/rc4.d/S85sd-agent
ln -s /etc/rc.d/init.d/sd-agent /etc/rc.d/rc5.d/S85sd-agent 
ln -s /etc/rc.d/init.d/sd-agent /etc/rc.d/rc6.d/K15sd-agent
pbr
  • 206
  • 1
  • 3
1

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.

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
  • The issue seems to have been not clearing up the PID file from /tmp. I moved it to the proper location /var/run and the init script worked fine, without modification. I can't move the handling of the PID out of the Python script because it can be executed separately from the init script and in that case, the PID file needs to be created. Thanks. –  Aug 03 '09 at 13:35