4

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?

ᔕᖺᘎᕊ
  • 6,253
rubo77
  • 4,920
  • what do you mean by "stop it to restart it again with screen,tmux.."? if you start a tmux/scrn session via ssh, it will keep alive if you close your ssh connection. –  Oct 08 '12 at 13:29
  • the process is already running and lasting for some hours, but i cannot stop it or i will loose data. – rubo77 Oct 08 '12 at 14:46
  • If the command is already running, I don't think there's a way to accomplish this. There are, however, numerous ways to do it in the future if you run it under screen, dtach, tmux, etc. as you already note, or with appropriate redirections and nohup. You might be able to ^Z, bg it now, but whether that's enough depends on whether it does I/O on the terminal after you background it. –  Oct 08 '12 at 14:58

1 Answers1

7

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

Eun
  • 2,503
  • I would add 2>&1 to see the error output too. – Dmytro Sirenko Oct 08 '12 at 14:04
  • 2
    &> redirects all output :D –  Oct 08 '12 at 14:05
  • thx, that would be the right start in the first place. But you didn't read my question: the process is already running and cannot be started over with your additions – rubo77 Oct 08 '12 at 14:47
  • ok, edited, check my answer –  Oct 08 '12 at 14:52
  • yes, thats the question: "detatch from a process that is allready running" ;) So disown would be your answer? Isn't there a problem then with stdout being detached? see http://unix.stackexchange.com/questions/39714/why-do-nohup-and-disown-not-work-on-sox-invoked-as-play – rubo77 Oct 08 '12 at 15:23
  • 1
    brings me to this: http://serverfault.com/a/284795, Start a screen, run reptyr with pid and detach –  Oct 08 '12 at 15:32
  • disown doesen't work: "bash: disown: 4219: Kein solcher Job." And reptyr is a bit strange: i get lots of messages and at the end "[+] Set the controlling tty" Does this mean, that it worked? – rubo77 Oct 08 '12 at 20:45
  • actually reptyr works fine: http://serverfault.com/a/284795 thanks! – rubo77 May 14 '13 at 17:35