Linux – Prevent Background Process from Stopping After SSH Close

gnu-screenlinuxsshtmux

I have a process running on my Linux machine (Debian squeeze) that takes hours (or days) to finish.

I don't want to stop it to restart it again with screen, tmux or with an output redirect to nohup.

Is there a secure way to put it in the background with ^Z and bg so it will continue once I close the ssh-session?

Best Answer

use

commandtoexecute &> /dev/null &

it will run your process in the background, and prints all output to /dev/null.

Replace /dev/null with another file to see the output.

e.g. commandtoexecute &> /tmp/file1 &

use tail -f /tmp/file1 to attach to output again

You can also redirect stdin, see this http://www.tuxfiles.org/linuxhelp/iodirection.html

If you want to detatch from a process that is allready running. Use disown <pid> where pid is your process id.

You could also change the terminal to another terminal:

  1. start a screen
  2. get pid of your process
  3. run reptyr <pid>
  4. detach using CTRL+A+D

reptyr: https://serverfault.com/a/284795

Related Question