20

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

user4951
  • 10,519

9 Answers9

33

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.

bsd
  • 11,036
  • 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 '14 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. – bsd Jan 12 '14 at 13:58
8

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.

Elias Probst
  • 1,053
7

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

  • 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 '14 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 '14 at 21:34
4
  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.
  • So even if terminal is death it'll still work> – user4951 Jan 12 '14 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 '14 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. – A.L Jan 12 '14 at 20:13
  • If your terminal becomes Death, I suspect you have bigger problems. – Schilcote Jan 12 '14 at 20:42
4

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.

peterph
  • 30,838
2

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
0

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.

slm
  • 369,824
0

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.

peterph
  • 30,838
0

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.