Start/Stop Script for Python Programs

 

For the last two years or so, I often find myself in the situation, where I need to quickly prototype a Web-service that is accessed by mobile applications. If successful, the service will eventually be hosted in Amazon’s AWS Elastic Compute Cloud, most likely in Red Hat Enterprise Linux 6 (EHEL 6) instance.

Besides a few other condition, depending on the estimated load requirements during prototyping and early testing, I either start implementing on a CentOS Linux box (CentOS is very close to being RHEL, without the branding and support), like the Intel NUC, at or close to my desk, or start with a small or medium size EC2 instance right away.

Python and Tornado (Tornado is a Python web framework and asynchronous networking library; it is one of Facebook’s open source technologies and available under the Apache License) are my preferred choices, when it comes to implementing the Web service and JetBrain’s PyCharm is the best IDE I could find, for coding and also rapid deployment.

Eventually however, the Python/Tornado application needs a start/stop script and also to be started on boot.
When it comes to stopping the app, simply killing it, might not always be the best option. I.e., the application needs to implement an exit handlerand also catch the kill event of course.

Exit Handler

import os,signal,sys
 
..
 
def set_exit_handler(func):
    signal.signal(signal.SIGTERM, func)
def on_exit(sig, func=None):
    print "exit handler triggered"
    sys.exit(1)
 
..
 
if __name__ == "__main__":
    set_exit_handler(on_exit)
..

Start/Stop Script for CentOS Linux (System V)

Here for instance is the start/stop script I use for an Text to Speech Synthesizer:

#!/bin/sh
# chkconfig: 123456 90 10
# TTS Server for Speech Synthesis
#
workdir=/etc/speech
 
start() {
    cd $workdir
    /usr/bin/python /etc/speech/TTSserver.py &
    echo "Server started."
}
 
stop() {
    pid=`ps -ef | grep '[p]ython /etc/speech/TTSserver.py' | awk '{ print $2 }'`
    echo $pid
    kill $pid
    sleep 2
    echo "Server killed."
}
 
case "$1" in
  start)
    start
    ;;
  stop)
    stop   
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: /etc/init.d/tornado-tts {start|stop|restart}"
    exit 1
esac
exit 0

Launch the script automatically when the system boots up

All System V init scripts are stored in /etc/rc.d/init.d or /etc/init.d directory. So this script then needs to be stored for instance in the /etc/init.d and also made executable and finally added with ckconfig:

chmod +x /etc/init.d/tornado-tts
/sbin/chkconfig tornado-tts on

Although, the line at the top containing chkconfig appears to be comment, it is used by chkconfig command and must be present. This particular line defines that on runlevels 1,2,3,4,5, and 6, the servers will be activated with priority 90 (one of the lasts), and deactivated with priority 10 (one of the firsts).

Runlevels

  1. Halt system
  2. Take system to single-user mode (good for Linux system maintenance)
  3. User defined or distribution like Debian use it
  4. Full multi-user mode (text mode login)
  5. Not used/user-defined
  6. Full multi-user GUI mode login
  7. Reboot system

 

 

Leave a Reply