Monday, September 2, 2013

Sample Initd.d Script .

Init Script


You can run the script by running python testdaemon.py start

This will spawn the daemon process. You can stop it by running daemon.py stop


The following init script will allow you to start and stop the daemon on system startup and shutdown respectively:


#! /bin/bash

# Copyright (c) 1996-2012 My Company.
# All rights reserved.
#

#
# /etc/init.d/testdaemon
#
### BEGIN INIT INFO
# Provides: testdaemon
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Test daemon process
# Description:    Runs up the test daemon process
### END INIT INFO


# Activate the python virtual environment
. /path_to_virtualenv/activate

case "$1" in
start)
echo "Starting server"
# Start the daemon
python /usr/share/testdaemon/testdaemon.py start
;;
stop)
echo "Stopping server"
# Stop the daemon

    python /usr/share/testdaemon/testdaemon.py stop

    ;;
restart)
echo "Restarting server"

    python /usr/share/testdaemon/testdaemon.py restart

    ;;
*)
# Refuse to do other stuff
echo "Usage: /etc/init.d/testdaemon.sh {start|stop|restart}"
exit 1
;;
esac

exit 0


Note that this is using python virtualenv to use an isolated python environment (please refer to my previous blog entryhere).


Save file in /etc/init.d/testdaemon

chmod u+x /etc/init.d/testdaemon

Enable using chkconfig testdaemon on

Check that it is enabled chkconfig --list

...

testdaemon                0:off  1:off  2:off  3:on   4:off  5:on   6:off

...

Kick it off by running service testdaemon start

 

No comments:

Post a Comment