How to make python script keep running even when the terminal is closed? Like a line in the actual .py code.
Asked
Active
Viewed 1,175 times
0
-
I don't think there's one - if you're on UNIX just use `screen` or `tmux` – Grzegorz Skibinski Sep 09 '20 at 07:31
-
does this answer your question? https://stackoverflow.com/questions/27624850/launch-a-completely-independent-process – bb1950328 Sep 09 '20 at 07:33
1 Answers
1
Run the command with nohup.
nohup pythonScript.py
nohup - run a command immune to hangups, with output to a non-tty -man pages o nohup
Note that output of the command will be appended to a file called nohup.out unless you redirect it (nohup pythonScript.py > yourfile.txt).
if you want to run in the background then use this nohup pythonScript.py &

Jatin Mehrotra
- 9,286
- 4
- 28
- 67
-
-
why do you need to put in inside the code run your program from the terminal using this command,it will solve your purpose,because its the shell's child process when it run from terminal , if parent process terminates, so does the child process,so it wont serve your purpose if you add it like a code. – Jatin Mehrotra Sep 09 '20 at 08:28
-
-
Add the detail to in your readme file that "how to run script" ? by saying run it with nohup simple or a better way in your python program add a line of code to make a alias of this command so next time whever they run it automatically run with nohup even if they simply run python script without nohup alias run script=nohup /path/to/pythonscript add this line to your python code. note alias should always be in .bashrc file to make it persistent, otherwise it will be avaialble only till current shell is there, hence have a way to add it into .bashrc file from your python program – Jatin Mehrotra Sep 10 '20 at 02:07
-
If this answer helped you in any way accept it as your solution so that it may help others in future, thanks :) – Jatin Mehrotra Sep 10 '20 at 02:08