views:

95

answers:

5

I have Python script bgservice.py and I want it to run all the time, because it is part of the web service I build. How can I make it run continuously even after I logout SSH?

+5  A: 

Run nohup python bgservice.py & to get the script to ignore the hangup signal and keep running. Output will be put in nohup.out.

Ideally, you'd run your script with something like supervise so that it can be restarted if (when) it dies.

scompt.com
+1  A: 

You can nohup it, but I prefer screen.

Randy Proctor
+3  A: 

You could also use GNU screen which just about every Linux/Unix system should have.

If you are on Ubuntu/Debian, its enhanced variant byobu is rather nice too.

Dirk Eddelbuettel
Thanks, you made my day : apparently the best thing since sliced bread has been improved upon.
Peter Tillemans
That is the exact same definition I use too :) So what do we call that? Sliced _and toasted_ bread? ;-)
Dirk Eddelbuettel
A: 

The zsh shell has an option to make all background processes run with nohup.

In ~/.zshrc add the lines:

setopt nocheckjobs  #don't warn about bg processes on exit
setopt nohup        #don't kill bg processes on exit

Then you just need to run a process like so: python bgservice.py &, and you no longer need to use the nohup command.

I know not many people use zsh, but it's a really cool shell which I would recommend.

jonescb
+1  A: 

If you've already started the process, and don't want to kill it and restart under nohup, you can send it to the background, then disown it.

Ctrl+Z (suspend the process)

bg (restart the process in the background

disown %1 (assuming this is job #1, use jobs to determine)

Cooper