14

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?

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.

  • 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 Feb 21 '10 at 17:18
  • 1
    nohup 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 Feb 21 '10 at 17:22
  • Ah found it. For screen, it's ctrl-A d to detach and -r +pid to reattach. – Ritwik Bose Feb 21 '10 at 17:23
  • Good answer. You should add the screen comment to you answer so that it stands out more. – DaveParillo Feb 21 '10 at 22:13
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!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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