Ubuntu – How to run script via SSH that doesn’t end when I close connection

bashgnu-screennohupsshUbuntu

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.

Best Answer

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.