nohup not working in terminal (macos) (running python)

237 views Asked by At

I recently tried running python programs in the background without having terminal or other application open, I tried using "nohup" in front of the terminal command which would run the python script (command used: nohup python3 -u "path would be here" > out &) but it still terminates the program whenever I close terminal. I just want to know if ther eis something i'm doing wrong or is there another method to run a python script without applications open in the background. Any help would be appreciated.

I attempted to use diffrent files and to remove parts such as the "> out" and looking online but didn't find anything.

Edit: the code I'm running is:

import time
x = 1
while 1 = 1:
   x = x + 1
   print(x)
   time.sleep(1)

(it works if I run it from terminal but don't close it)

1

There are 1 answers

1
Fenryl On

Another option to the nohup [...] & is to run the command/job as usual and disown it.

$ python3 -u "path would be here" > out &
$ disown

This post explains the differences between &, nohup and disown.

Another more complete answer than mine can be found here. They describe multiple ways to disassociate the process from the shell. That includes nohup, disown and Ctrl + D Logout.