How to add a python program to run on startup the cli way

To make a python program run on startup..first create o file on /etc/init.d..am creating a file named timestamp.

touch /etc/init.d/timestamp

Now edit the file

vim  /etc/init.d/timestamp

and enter the following

#! /bin/bash
# Copyright (c) 1996-2012 My Company.
# All rights reserved.
#
# Author: Ganesh H, 2013
#
# Please send feedback to ganeshredcobra@gmail.com
#
# /etc/init.d/timestamp
#
### BEGIN INIT INFO
# Provides: timestamp
# Required-Start: 
# Should-Start: 
# Required-Stop: 
# Should-Stop:
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 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 
    /usr/bin/python /home/ganesh/Pictures/timestamp.py start
    ;;
  stop)
    echo "Stopping server"
    # Stop the daemon
    /usr/bin/python /home/ganesh/Pictures/timestamp.py stop
    ;;
  restart)
    echo "Restarting server"
    /usr/bin/python /home/ganesh/Pictures/timestamp.py restart
    ;;
  *)
    # Refuse to do other stuff
    echo "Usage: /etc/init.d/timestamph {start|stop|restart}"
    exit 1
    ;;
esac

exit 0

give the correct path of the file..Now make the init.d file executable

chmod +x /etc/init.d/timestamp

Now add the script to bootup

sudo update-rc.d timestamp  defaults

or you can use chkconfig

chkconfig --add timestamp

To remove the startup

rm -fv /etc/rc*/*timestamp

or

chkconfig --del timestamp

To view all configured startup use the below command

chkconfig --list

Happy Hacking 🙂

Advertisements

2 thoughts on “How to add a python program to run on startup the cli way

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s