Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Service / Daemon

I'm writing a Python script that I need to have start at boot (it should continue running forever once it is started). I would like to be able to manage the service via running a command like:

sudo service my-service (and either start, stop, restart, etc.)

I have been reading a lot into it and would really like to finish my project. I saw this but would need help implementing it if it were to work Linux Start-up Script. I'm not even sure where to start, I don't really know how to program in bash but I'm open to all solutions. Thank you ahead of time, and I appreciate all responses!

like image 770
KosherBacon Avatar asked Aug 12 '26 01:08

KosherBacon


1 Answers

Take a look at zdaemon. It provides a simple way to daemonize a Python process.

Then you can write an init.d script for it - based on your OS. Or you can use a tool like Upstart, supervisord to control the daemon.

My init.d script (on centos 5.8) looks like this:

. /etc/rc.d/init.d/functions

. /etc/sysconfig/network

APP_PATH=/path/to/your/app
PYTHON=/usr/local/bin/python
USER=user

start() {
    cd $APP_PATH
    zdaemon -C app.zdconf start
}

stop() {
    cd $APP_PATH
    zdaemon -C app.zdconf stop
}

check_status() {
        cd $APP_PATH
        zdaemon -C app.zdconf status
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        check_status
        ;;
  restart)
        stop
        start
        ;;
  *)

esac
exit 0

Where app.zdconf is the zdaemon configuration file.

like image 63
yprez Avatar answered Aug 14 '26 14:08

yprez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!