Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I am running my Python program from my ubuntu machine as follows -

root@dbx1145:/home/david/zookteck# python test.py

But if I close my ubuntu machine, then my whole program will get stopped.. Is there any way of running this python program in the background and if I close my terminal then still it keeps on running?

And also after running this program in the background, how do I find out my actual program whether it is still running or not if I am logging back again to that machine?

share|improve this question

marked as duplicate by Seth, Alvar, Avinash Raj, don.joey, Flimm Dec 28 '13 at 22:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
See here: askubuntu.com/questions/106351/… should answer your question. Also a possible duplicate. – RPi Awesomeness Dec 28 '13 at 3:21
    
And, when you run a program in the background with &, it will print a PID that you can search for with something like ps aux | grep PID. – Seth Dec 28 '13 at 3:22
    
@Seth Yes, that puts it in the background, but when the terminal is quit, the program goes away too... – RPi Awesomeness Dec 28 '13 at 3:23
    
@Seth: Everytime I do need to remember the PID to look for the process? – lining Dec 28 '13 at 3:23
    
@TechGeeky Yes. – RPi Awesomeness Dec 28 '13 at 3:24
up vote 17 down vote accepted

Use the shebang line in your python script. Make it executable using the command,

chmod +x test.py

Use no hangup to run a program in background even if you close your terminal.

nohup /path/to/test.py &

Do not forget to use & to put it in background.

To see the process again, use in terminal,

ps ax | grep test.py
share|improve this answer
    
Great but I guess you should add python after nohup i.e. nohup python /path/to/test.py... – Denny May 29 '15 at 11:39
    
@Denny both nohup python /path/to/test.py & or nohup /path/to/test.py & will work. For the latter you need to use the shebang line in your python script and make the script executable as shown above. – souravc May 29 '15 at 13:40
    
How to bring the script to the foreground? – Li Xinyang Jan 17 at 11:05
1  
@LiXinyang If you are using bash, fg will bring the script to the foreground. But that won't change the output redirection, which will still be going to nohup.out. If the command jobs cannot find see it, then it is no longer a child of this shell. If you do a ps you'll see it is now owned by process 1 (init). That cannot be "brought back to the foreground" because the foreground no longer exists. you can use screen which will enable you to attach/reattach. – souravc Jan 19 at 6:14
    
@souravc I am using ash shell in Synology NAS, which does not support jobs and fg. :( But I can see the script is running using ps. I think it is probably better using some other tool like Tmux. Thanks for your response and helpful answer. – Li Xinyang Jan 19 at 6:17

python test.py &

Will run the file in the background.

In order to find the running program you can use ps -e to list all running programs. You can use grep to find your particular program from the list.

share|improve this answer
2  
In this case program will be terminated if you close the terminal, but OP do not want it. – souravc Dec 28 '13 at 3:32
    
@souravc Nope, the program will become a child of init --user. At least if you use the gnome terminal. Therefore it will only terminate upon logging out. – user2049175 Dec 28 '13 at 4:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.