I've written a simple init script to start and stop a Python script as a service. I have to be explicit about the version of Python I'm running, because this is on a CentOS 5 box with Python 2.4 & 2.6 installed (both via yum).
Here's what I have so far:
#!/bin/sh
# chkconfig: 123456 90 10
workdir=/usr/local/bin/Foo
start() {
cd $workdir
/usr/bin/python26 $workdir/Bar.py &
echo "FooBar started."
}
stop() {
pid=`ps -ef | grep '[p]ython26 /usr/local/bin/Foo/Bar.py' | awk '{ print $2 }'`
echo $pid
kill $pid
sleep 2
echo "FooBar stopped."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: /etc/init.d/foobar {start|stop|restart}"
exit 1
esac
exit 0
So:
1) I want to be "smarter" about the filename and directory name management, and set some variables up to so that anything repeated later in the script (like workdir). My main problem is the grep statement, and I haven't figured out how to deal with the variables inside the grep. I'd love any suggestions of a more efficient way to do this.
2) I want to add "status" support to this init script and have it check to see if the Bar.py is running.
Bar.py? Why not usepgrep? Why is the python version relevant?