23

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.

Francozen
  • 163
  • 5
Jakub Arnold
  • 1,674
  • 10
  • 25
  • 33

6 Answers6

28

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.

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
10

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
m0j0
  • 231
  • 1
  • 6
8

To install screen : apt-get install screen

Then launch using simply : screen

And man screen to get the key binding.

Disco
  • 1,301
  • 5
  • 19
  • 34
  • +1! screen is da thing! – Javier Oct 21 '09 at 21:45
  • I used to use screen, but have switched to tmux for a variety of reasons. Google "tmux vs screen" and you will find some links like this: https://superuser.com/questions/236158/tmux-vs-screen – m0j0 Oct 20 '17 at 17:18
  • screen/tmux/byobu does much more than preventing the death of running script, a indispensable tool! – Francozen Feb 27 '20 at 16:15
2

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 &
Michael Dillon
  • 1,809
  • 13
  • 16
0

Trivia: The command name nohup comes from "hang-up," as in hanging up the (of course, "black, rotary-dial" ...) phone on your 110-baud Teletype machine after removing the headset from its squishy holder. Usually, when Unix detects that you "hung up," it sends a SIGHUP signal to the process.

-1

If you want something you can run non-interactively from the client, try this:

ssh user@host 'command >out.log 2>err.log &'

For example:

$ ssh user@host '(while true; do date; sleep 1; done) >/tmp/dates &'
$ ssh user@host
...
user@host$ tail -f /tmp/dates
user@host$ ps -ef | grep dates

Note however that if you don't redirect the output to files then the ssh invocation won't finish until the command does.

ijt
  • 99
  • 2