Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm sshing into a server and I'm starting a Python script that'll take approx. 24 hours to complete. What if my internet connection dies in the middle? Will that stop the command?

Is there any way to run my long-running command in a way that local disconnects won't affect it and I could continue to see its output after I log in to ssh again?

share|improve this question

3 Answers 3

up vote 23 down vote accepted

The best way is to use screen to start a session to run the command in and then disconnect the screen so it will keep running, and you can do other things, or just disconnect from the server. The other option is to use nohup in combination with & so you would have nohup <command> &

share|improve this answer
    
Screen. Or you can setup a cron task if its something you run more then once. –  xeon Aug 19 '10 at 19:54
2  
If you get disconnected while it's running in screen, screen -r will reconnect you. –  Viaken Aug 19 '10 at 21:41

You can also use disown if you've already started the process without screen or nohup

share|improve this answer

If you background a process & close your session the process will get adopted by init (PID 1).

If I have a session and do:

]$ sleep 10000 &
]$ exit

and open another session and run:

]$ ps -ef | grep sleep
501      26387     1  0 16:37 ?        00:00:00 sleep 10000
root     26422 21360  0 16:40 pts/0    00:00:00 grep sleep

My process is still running, and we see that its parent process is now 1 (init).

Alternatively you could set up your script like you would a daemon. A quick search turns up this seemingly useful link: http://onlamp.com/python/pythoncook2/solution.csp?day=1. If you wanted to take that approach.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.