Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run python script as daemon at boot time (Ubuntu)

I've created small web server using werkzeug and I'm able to run it in usual python way with python my_server.py. Pages load, everything works fine. Now I want to start it when my pc boots. What's the easiest way to do that? I've been struggling with upstart but it doesn't seem to "live in a background" cuz after I execute start my_server I immediately receive kernel: [ 8799.793942] init: my_server main process (7274) terminated with status 1

my_server.py:

...
if __name__ == '__main__':
    from werkzeug.serving import run_simple
    app = create_app()
    run_simple('0.0.0.0', 4000, app)

upstart configuration file my_server.conf:

description "My service"
author  "Some Dude <[email protected]>"
start on runlevel [2345]
stop on runlevel [016]
exec /path/to/my_server.py
start on startup

Any Ideas how to make it work? Or any other better way to daemonize the script?

Update: I believe the problem lies within my_server.py. It doesn't seem to initiate the webserver (method run_simple()) in the first place. What steps should be taken to make .py file be run by task handler such as upstart?

  • Place shebang as first line #!/usr/bin/env python
  • Allow execution permissions chmod 755
  • Start the daemon with superuser rights (to be absolutely sure no permission restrictions prevents it from starting)
  • Make sure all python libraries are there!
  • Something else?

Solved: The problem was with missing python dependencies. When starting the script through task manager (e.g. upstart or start-stop-daemon) no errors are thrown. Need to be absolutely sure that pythonpath contains everything you need.

like image 746
and3p Avatar asked Dec 05 '12 08:12

and3p


People also ask

How do I run a Python script on Ubuntu?

Running a ScriptOpen the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T . Navigate the terminal to the directory where the script is located using the cd command. Type python SCRIPTNAME.py in the terminal to execute the script.

How do I run a daemon process in Python?

Daemon processes in Python To execute the process in the background, we need to set the daemonic flag to true. The daemon process will continue to run as long as the main process is executing and it will terminate after finishing its execution or when the main program would be killed.


2 Answers

In addition to gg.kaspersky method, you could also turn your script into a "service", so that you can start or stop it using:

$ sudo service myserver start
 * Starting system myserver.py Daemon                          [ OK ]
$ sudo service myserver status
 * /path/to/myserver.py is running
$ sudo service myserver stop
 * Stopping system myserver.py Daemon                          [ OK ]

and define it as a startup service using:

$ sudo update-rc.d myserver defaults

To do this, you must create this file and save it in /etc/init.d/.

#!/bin/sh -e

DAEMON="/path/to/myserver.py"
DAEMONUSER="myuser"
DAEMON_NAME="myserver.py"

PATH="/sbin:/bin:/usr/sbin:/usr/bin"

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

d_start () {
        log_daemon_msg "Starting system $DAEMON_NAME Daemon"
        start-stop-daemon --background --name $DAEMON_NAME --start --user $DAEMONUSER --exec $DAEMON
        log_end_msg $?
}

d_stop () {
        log_daemon_msg "Stopping system $DAEMON_NAME Daemon"
        start-stop-daemon --name $DAEMON_NAME --stop --retry 5 --name $DAEMON_NAME
          log_end_msg $?
}

case "$1" in

        start|stop)
                d_${1}
                ;;

        restart|reload|force-reload)
                        d_stop
                        d_start
                ;;

        force-stop)
               d_stop
                killall -q $DAEMON_NAME || true
                sleep 2
                killall -q -9 $DAEMON_NAME || true
                ;;

        status)
                status_of_proc "$DAEMON_NAME" "$DAEMON" "system-wide $DAEMON_NAME" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
                exit 1
                ;;
esac
exit 0

In this example, I assume you have a shebang like #!/usr/bin/python at the head of your python file, so that you can execute it directly.

Last but not least, do not forget to give execution rights to your python server and to the service script :

$ sudo chmod 755 /etc/init.d/myserver
$ sudo chmod 755 /path/to/mserver.py

Here's the page where I learned this originally (french).

Cheers.

like image 63
tvuillemin Avatar answered Oct 17 '22 07:10

tvuillemin


One simple way to do is using crontab:

$ crontab -e

A crontab file will appear for editing, write the line at the end:

@reboot python myserver.py

and quit. Now, after each reboot, the cron daemon will run your myserver python script.

like image 43
kaspersky Avatar answered Oct 17 '22 06:10

kaspersky