0

How to make python script keep running even when the terminal is closed? Like a line in the actual .py code.

Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67
iBikky
  • 1
  • 1

1 Answers1

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
  • where do I put this in the code? – iBikky Sep 09 '20 at 08:16
  • 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
  • because when other people run it they will not know to do that – iBikky Sep 09 '20 at 22:47
  • 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