Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

In Linux. Say I want to run a command and it takes too long. I just want to ensure it's done. How?

share|improve this question
add comment

marked as duplicate by Gilles, slm, rahmu, jasonwryan, Anthon Jan 13 at 5:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

9 Answers

nohup

read the man page for nohup usage.

nohup is the way it's been done long since before screen, tmux, etc were invented.

Example:

nohup my_long_running_proc &

Runs "my_long_running_proc", and any console (stdout/stderr) messages go into a file called "nohup.out" in the directory from which the command was started.

share|improve this answer
1  
true. The init process kills all children of a process before killing the process itself, with the exception of processes started with nohup. These processes will then be moved under init. –  Thorsten Staerk Jan 12 at 13:52
    
I've been using nohup since the days when I connected to shell account via modem, wanted long running command to stay running and complete so I could logout and hangup not incurring phone charges, or having modem disconnect midway. Tried and true, decades of testing. –  bdowning Jan 12 at 13:58
add comment

Use tmux or screen to provide a persistent session environment for the command to run in.

Using tmux, this could be accomplished in the following way:

  • Start a new session environment: tmux new -s my-session-name
  • Run your command: longrunningcommand
  • Close your terminal window, SSH session or manually detach from tmux using Ctrl+b, then d
  • Reconnect to your session using: tmux attach-session -t my-session-name

tmux can do way more beyond that, but one basic thing which should probably be added: Use tmux list-sessions to see all active sessions.

share|improve this answer
add comment

Test it with the command xclock. Open a console, type

xclock

Close the console. xclock disappears. Now type

xclock &

xclock still disappears cause it is still a sub-process of your shell. Now type

xclock & disown

Now xclock is no longer a sub-process of your shell and you can close the console and xclock will keep running.

I documented this here: http://www.linuxintro.org/wiki/Disown

share|improve this answer
    
This doesn't let you check the output and/or exit status of the program - which is a rather important part of the question. –  peterph Jan 12 at 20:45
1  
Well actually the question doesn't say anything about checking output or exit status. True, there are many cases where that is important information, but this may not be one of them. –  David Z Jan 12 at 21:34
add comment
  1. Use screen (man screen)
  2. append "&" at the end of the command. It will be launched into background and if You kill the terminal session I think it should complete. Anyway I recommend to always use screen.
share|improve this answer
    
So even if terminal is death it'll still work> –  Jim Thio Jan 12 at 12:31
    
with screen -> 100%. With the & launch in background I am not 100% sure. You can launch a command with & (like sleep 1000). kill terminal and from another terminal check ps auwx|grep sleep –  Bartłomiej Zarzecki Jan 12 at 12:43
    
You don't need to append & if you use screen. Just hit Ctrl + A then Ctrl + D to leave the screen session. And the command you launched is still running. –  n.1 Jan 12 at 20:13
    
If your terminal becomes Death, I suspect you have bigger problems. –  Schilcote Jan 12 at 20:42
add comment

When you log out of your shell, any still running chldren are sent a SIGHUP signal. For your process to continue to run after logout, it needs to trap, ignore or avoid this signal, since the default behaviour is to exit.

The nohup command will ignore the signal, and is a common way to do it if the process does not send anything important to stdout.

The screen command (which is my preference) creates a shell-within-a-shell that can be detatched and reattached, so it avoids the signal being generated at all (or rather, it detatches from the parent so it never gets the signal).

If the process is written by yourself, you can code it to catch the signal and then perform any action, including ignoring it, so that it continues to run after logging out. Your process could also completely disassociate from its parent process (see setsid()) so that, as with screen, it does not receive the SIGHUP during logout.

Which method you choose is up to you and which is more appropriate for your situation and use.

share|improve this answer
add comment

With bash 3.2, I found that disown -h works best. For example, to prevent gedit from closing when the terminal closes, I need to both put it in the background and use disown. It is easy to do this via a function in ~/.bashrc:

ge() { gedit "$@" & disown -h; }  # need double--not single--quotes
share|improve this answer
add comment

use tmux:

sudo apt-get install tmux

To start it simply type tmux and to minimize it press Ctrl+b and then d to start the same session type tmux attach.

share|improve this answer
1  
A bit Ubuntu specific instructions - not everyone is using these. –  peterph Jan 12 at 21:01
    
@peterph user did not mention what distro he/she is using –  Khan Shahrukh Jan 12 at 21:04
    
... and that's exactly my point. –  peterph Jan 12 at 21:06
add comment

Redirect the command's stdout and stderr to a file, save its exit value and then run it in nohup or (better) under a terminal multiplexer like tmux or screen:

command 1> command.out 2> command.err; echo $? > command.exit

To check the progress you can simply tail -f the log files. The point of logging into files is that terminal multiplexers have a limited buffers (although one can usually set it to be large enough for most tasks) and they might crash, in which case all the output would be lost.

share|improve this answer
add comment

I very small but efficient tool that substitute tmux and screen well is dtach, it strips out all those complicated windows management functions but only serve to preserve the job in the back and bring it foreground at any time you like.

share|improve this answer
add comment

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