简体   繁体   中英

Run python script in background on server and get script output

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!

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

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

disown -a

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)

Just look here :

python3 yourscript.py >> out.txt & disown

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM