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 need to run script, that takes long time to execute, or I just want it to run forever. I can't just SSH to my machine, because when I disconnect it stops running.

Is there any way to run script that isn't dependent on the shell that started it?

I'm using Ubuntu 9.04.

share|improve this question

4 Answers 4

up vote 15 down vote accepted

You can run the command with the nohup command before it. You can also run it in 'screen', which will allow you reattach the terminal.

For example:

ssh mySever 'nohup bash myscript.sh'

Or just ssh into and run the nohup command. It should keep running even when you disconnect. This is because the nohup will intercept the SIGHUP singal (hangup).

Screen is a bit more involved, but for the 20 minutes it might take you to learn the basics, it is one of the most useful tools out there. Here is a tutorial.

share|improve this answer
    
Sniped by Kyle... >smile< –  Evan Anderson Oct 21 '09 at 18:16
    
+1 to anyone who snipes Evan. Happy 10K :) –  squillman Oct 27 '09 at 0:54
    
squillman: Thanks :-) –  Kyle Brandt Oct 27 '09 at 11:30

To install screen : apt-get install screen

Then launch using simply : screen

And man screen to get the key binding.

share|improve this answer
    
+1! screen is da thing! –  Javier Oct 21 '09 at 21:45

I always just use nohup. If I care about the output, I redirect it to a log file, otherwise I send it to /dev/null.

Example:

nohup scp file.tar.gz root@someserver:/var/tmp > /dev/null 2>&1 &

That puts it in the background, with output going to /dev/null, and it is immune to the HUP signal if you log out. You can also "disown" the job with certain shells (like bash) so that it's not connected to your session.

disown %1
share|improve this answer
    
+1 for disown - very useful when you forget to run using nohup –  Mark Oct 21 '09 at 21:10

While you are logged into a server, you can run a script in detached mode by the following command:

nohup script >script.out 2>script.err &

Later when you log in again, you can check script.out for any output, and script.err for any error messages. If you want output and error messages in the same file, then do:

nohup script >script.out 2>&1 &
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.