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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With