1

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.

4
  • Maybe this can help you: [starting and stopping a daemon at user login logout][1] [1]: stackoverflow.com/questions/3950585/… Commented Mar 1, 2012 at 17:28
  • i use "sudo /etc/init.d/my_python start" Commented Mar 1, 2012 at 17:30
  • I would have thought start-stop-daemon takes care of everything... obviously it does not. Try trapping the SIGHUP signal in the Python script. Redirecting the output to a file or /dev/null may also be wise. Commented Mar 1, 2012 at 17:39
  • i have tried that too. it does not work. Commented Mar 1, 2012 at 17:45

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

the daemon still stops after i logout.
wRAR is there any other way to run python script in background ?
screen, nohup, & + disown etc. Anyway, the only proper way is to write a script properly as daemon and write a proper initscript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.