Python Daemon Packaging Best Practices

后端 未结 10 1436
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 09:48

I have a tool which I have written in python and generally should be run as a daemon. What are the best practices for packaging this tool for distribution, particularly how sho

10条回答
  •  执笔经年
    2021-01-30 10:13

    The best tool I found for helping with init.d scripts is "start-stop-daemon". It will run any application, monitor run/pid files, create them when necessary, provide ways to stop the daemon, set process user/group ids, and can even background your process.

    For example, this is a script which can start/stop a wsgi server:

    #! /bin/bash
    
    case "$1" in
      start)
        echo "Starting server"
    
        # Activate the virtual environment
        . /home/ali/wer-gcms/g-env/bin/activate
    
        # Run start-stop-daemon, the $DAEMON variable contains the path to the
        # application to run
        start-stop-daemon --start --pidfile $WSGI_PIDFILE \
            --user www-data --group www-data \
            --chuid www-data \
            --exec "$DAEMON"
        ;;
      stop)
        echo "Stopping WSGI Application"
    
        # Start-stop daemon can also stop the application by sending sig 15
        # (configurable) to the process id contained in the run/pid file
        start-stop-daemon --stop --pidfile $WSGI_PIDFILE --verbose
        ;;
      *)
        # Refuse to do other stuff
        echo "Usage: /etc/init.d/wsgi-application.sh {start|stop}"
        exit 1
        ;;
    esac
    
    exit 0
    

    You can also see there an example of how to use it with a virtualenv, which I would always recommend.

提交回复
热议问题