Run python script as daemon at boot time (Ubuntu)

前端 未结 3 1874
你的背包
你的背包 2020-12-13 05:14

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 wan

相关标签:
3条回答
  • 2020-12-13 05:42

    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.

    0 讨论(0)
  • 2020-12-13 05:58

    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.

    0 讨论(0)
  • 2020-12-13 06:03

    If you have supervisor service that starts at boot, write a supervisor service is much, much simpler.

    You can even set autorestart if your program fails.

    0 讨论(0)
提交回复
热议问题