Python Launcher

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 handler_and 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"


if __name__ == "__main__":

Start/Stop Script for CentOS Linux (System V)

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

# chkconfig: 123456 90 10
# TTS Server for Speech Synthesis

start() {
    cd $workdir
    /usr/bin/python /etc/speech/ &
    echo "Server started."

stop() {
    pid=`ps -ef | grep '[p]ython /etc/speech/' | awk '{ print $2 }'`
    echo $pid
    kill $pid
    sleep 2
    echo "Server killed."

case "$1" in
    echo "Usage: /etc/init.d/tornado-tts {start|stop|restart}"
    exit 1
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).


  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