简体   繁体   中英

Python script as daemon in ubuntu

I created a daemon to run a python script. but it stops whenever i logout from my ubuntu machine.

DAEMON=sudo python /var/www/some_dir/my_python.py
ARGS=/var/www/some_dir/my_python.py
PIDFILE=/var/www/some_dir/my_python.pid

test -x $DAEMON || exit 0

#set -e

case "$1" in
start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON &
    echo "$NAME."
    ;;
stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --pidfile $PIFDILE --exec $DAEMON
    echo "$NAME."
    ;;
restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --pidfile $PIDFILE --exec $DAEMON
    sleep 1
    start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON &
    echo "$NAME."
    ;;
*)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac
exit 0

can anyone tell me how can i do it.

It's a long time since this question was asked, but I came across this situation today.

To start the process in the background, use

start-stop-daemon -Sbm --pidfile $PIDFILE --exec $DAEMON

To stop it:

start-stop-daemon -K --pidfile $PIDFILE

From the start-stop-daemon man page :

-b , -background

Force the daemon into the background. Some daemons don't create pidfiles, so a good trick is to get the daemon to run in the foreground, and use the this option along with -m , -make-pidfile to create a working pidfile. -m , -make-pidfile

Saves the pid of the daemon in the file specified by the -p, -pidfile option. Only useful when used with daemons that run in the foreground and forced into the background with the --b, -background option.

Try to remove & from your start-stop-daemon invocations. Also, you should read how to write proer initscripts because there are several other errors in your initscript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM