Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running python code in background

I need to run a python code that takes several hours and my computer disconnects from the ssh after a certain amount of inactive time.

I have tried python test.py > output.txt & but my output file is empty. However, the python code "test" is still running after I log off and log back in to the ssh. I also tried python -u test.py > output.txt & which does write to the output.txt but it does not continue after the ssh connection is lost.

I am very new to Linux so I do not know very many commands. I need the simplest/easiest to understand method. Thanks!

like image 319
user2917930 Avatar asked Feb 27 '26 06:02

user2917930


1 Answers

You can use screen, as Robin Krahl recommended, or you can just run your command with nohup, which suppresses the SIGHUP (hangup) signal from your SSH session disconnecting.

nohup "python -u test.py > output.txt" &

like image 189
Christian Ternus Avatar answered Feb 28 '26 20:02

Christian Ternus