Leave bash script running on remote terminal while not logged in?

14

6

I have a bash script that takes several hours to run. While it's running, I would like to do other things, which may involve logging out or disconnecting from the internet (my script runs network tests on various computers).

I understand that there is a command that would allow me to run my tests from a remote terminal and logout of the terminal while it runs. Does anyone know what this command is?

Ritwik Bose

Posted 2010-02-21T17:07:30.077

Reputation: 828

Answers

17

The command you are looking for is nohup.

There is also screen, which is for when you want to leave something running but come back and reattach interactively later.

nohup is simpler if a command expects no user input after launch, and screen is better if you want to be able to run the program interactively.

Justin Smith

Posted 2010-02-21T17:07:30.077

Reputation: 3 746

I looked at nohup right now and apparently there are issues with input and output which might cause the script to hang up. I also saw something about screen, which strikes me as what I had read about before. Do you know how to use either of these? – Ritwik Bose – 2010-02-21T17:18:21.890

1nohup is for when a script expects no user input - it saves all output to a file. Screen is for when you want to leave something running but come back and reattach interactively later. nohup is simpler if a command expects no user input after launch, and screen is better if you want to be ably to run the program interactively. – Justin Smith – 2010-02-21T17:22:31.600

Ah found it. For screen, it's ctrl-A d to detach and -r +pid to reattach. – Ritwik Bose – 2010-02-21T17:23:39.367

Good answer. You should add the screen comment to you answer so that it stands out more. – DaveParillo – 2010-02-21T22:13:01.390

1

I use nohup for this. In my case I had a python script named action.py I ran the script on a remote server with nohup python action.py & I would then close the terminal. Later when it's finished running, you can see all output from the process in nohup.out which will be created in the same directory as action.py. If the file already exists further output will be appended to it.

The only snag is not knowing when the script has run its course, so in my case I had it shoot me an email upon completion. Hope this helped someone!

Bryan J

Posted 2010-02-21T17:07:30.077

Reputation: 11

0

What happens if you forgot to add the nohup Command at the beginning of your Command?

Let us say that you have run the following Long Command:

The beginning of the Output of a Long Command

This is called as ls -Ral / that is supposed to list all of the Files and all of the Folders, including the Hidden ones, with a Long Format, Recursively starting from the Root Folder. It might take a while.

Just press ctrl+Z.

Stopping the Long Command

How do you get it back?

The Command to get it back into the Foreground is fg.

Bringing the Command back

user1018743

Posted 2010-02-21T17:07:30.077

Reputation: