-1

So, I need to run a python script on a server but I want the script to output whatever it outputs into a file so I can check what it done after it finished. However I also need the script to continue running if the ssh tunnel to the server gets interrupted or closed. Here is what I know:

python3 run.py >> out.txt
will output correctly.

nohup python3 run.py
will output correctly and be safe from interruption. it will also output into a nohup.out but will not run in the background of the current session (thus blocking me from doing anything else).

So I would think

nohup python3 run.py >> out.txt &
would work for me but it does not.
I've tried different combinations of what I want but just cannot get it to work... once I add & it appears the output to the files just stops going. I have no clue why though. any help appreciated!

  • You are probably experiencing this buffering issue: https://stackoverflow.com/questions/12919980/nohup-is-not-writing-log-to-output-file – VPfB Apr 16 '20 at 09:27
  • @VPfB aha! thank you that seemed to be the problem! interesting issue but glad its solved. – Eoghan Hogan Apr 16 '20 at 09:43
  • I'm glad I could help. The moderators will now almost certainly close this as a duplicate. Anyway, welcome to SO. – VPfB Apr 16 '20 at 11:54
  • Does this answer your question? [Nohup is not writing log to output file](https://stackoverflow.com/questions/12919980/nohup-is-not-writing-log-to-output-file) – VPfB Apr 16 '20 at 11:55

4 Answers4

0

What you are looking for is been already answered here

https://askubuntu.com/questions/8653/how-to-keep-processes-running-after-ending-ssh-session

You need to use tmux to keep it running in background

Shivam ashtikar
  • 1,188
  • 1
  • 7
  • 7
  • Thats not exactly what I asked though. I can get it running after ending the ssh session its the output of my script that is the problem, – Eoghan Hogan Apr 16 '20 at 09:20
0

The easiest way is to run your command in the background with &. Then just write:

disown -a
0

for anyone experiencing similar issues there is problem with nohup and python output buffering I guess.
see: Nohup is not writing log to output file.

but the fix is:

nohup python3 -u run.py >> out.txt &

(Answer provided by @VPfB)

0

Just look here:

python3 yourscript.py >> out.txt & disown
Tim
  • 406
  • 3
  • 14
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30011751) – warped Oct 06 '21 at 21:10