66

I am running a python program from a terminal in my Ubuntu machine as,

$ python test.py

But my whole program will be stopped if I close the terminal, Is there any way of running this python program in the background so that if I close my terminal then it still 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 terminal?

sourav c.
  • 44,715
arsenal
  • 1,993

2 Answers2

105

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

chmod +x test.py

Use no hangup to run the program in the background even if you close your terminal,

nohup /path/to/test.py &

or simply (without making any change in your program)

nohup python /path/to/test.py &

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

Role of nohup: nohup makes your script ignore SIGHUP, and redirects stdout/stderr to a file nohup.out, so that the command can continue running in the background after you log out. If you close the shell/terminal or log off, your command is no longer a child of that shell. It belongs to init process. If you search in pstree you'll see it is now owned by process 1 (init).

To see the process again, use in terminal,

ps ax | grep test.py

That cannot be brought back to the foreground because the foreground (as the terminal already closed) no longer exists. So there is no way to get that terminal back again once it is closed.

sourav c.
  • 44,715
  • 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
  • 2
    @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. – sourav c. May 29 '15 at 13:40
  • How to bring the script to the foreground? – XYZ Jan 17 '16 at 11:05
  • 3
    @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. – sourav c. Jan 19 '16 at 06: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. – XYZ Jan 19 '16 at 06:17
  • screen (as @souravc mentions) or tmux is what you want : https://askubuntu.com/a/8657/169878 or https://superuser.com/a/632219/47628 . Many more advantages than a nohup. – B. Shea Nov 19 '17 at 17:53
  • How would one close the task once it is opened in the background? – Stevoisiak May 25 '21 at 20:45
  • The program usually ends on its own after doing some specific task, or you may close the program using any of the program's quitting options. Else, you have to kill it from pstree. – sourav c. May 26 '21 at 04:00
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.

en4bz
  • 712
  • 9
    In this case program will be terminated if you close the terminal, but OP do not want it. – sourav c. Dec 28 '13 at 03:32
  • 1
    @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. – en4bz Dec 28 '13 at 04:43