In Linux. Say I want to run a command and it takes too long. I just want to ensure it's done. How?
read the man page for
nohup is the way it's been done long since before
tmux, etc were invented.
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.
screen to provide a persistent session environment for the command to run in.
tmux, this could be accomplished in the following way:
- Start a new session environment:
tmux new -s my-session-name
- Run your command:
- Close your terminal window, SSH session or manually detach from
- 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.
Test it with the command xclock. Open a console, type
Close the console. xclock disappears. Now type
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
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.
nohup command will ignore the signal, and is a common way to do it if the process does not send anything important to
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.
Redirect the command's
stderr to a file, save its exit value and then run it in
nohup or (better) under a terminal multiplexer like
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.