How can I disown a running process and associate it to a new screen shell?
I have a running program on a SSH shell. I want to pause it and be able to unpause its execution when I come back.
One way I thought of doing that was to transfer its ownership to a screen shell, thus keeping it running in there.
Is there a different way to proceed?
You can revoke “ownership” of the program from the shell with the disown
built-in:
# press Ctrl+Z to suspend the program
bg
disown
However this only tells the shell not to send a SIGHUP
signal to the program when the shell exits. The program will retain any connection it has with the terminal, usually as standard input, output and error streams. There is no way to reattach those to another terminal. (Screen works by emulating a terminal for each window, so the programs are attached to the screen window.)
It is possible to reattach the filedescriptors to a different file by attaching the program in a debugger (i.e. using ptrace
) and making it call open
, dup
and close
. There are a few tools that do this; this is a tricky process, and sometimes they will crash the process instead. The possibilities include (links collected from answers to How can I disown a running process and associate it to a new screen shell? and Can I nohup/screen an already-started process?):
I don’t use it regularly, but neercs claims to support this. It’s a screen
-like program with miscellaneous fancy features like better pane management, but the main thing it offers is the ability to import a process into a pane
There’s also a small utility called retty that lets you reattach running programs to another terminal.
Using GNU screen
is your best bet.
Start screen running when you first login – I run screen -D -R
, run your command, and either disconnect or suspend it with CTRL-Z
and then disconnect from screen by pressing CTRL-A
then D
.
When you login to the machine again, reconnect by running screen -D -R
. You will be in the same shell as before. You can run jobs
to see the suspended process if you did so, and run %1
(or the respective job #) to foreground it again.
To move a process between terminals or to reattach a disowned, you can use e.g. reptyr.
My favorite solution is using tmux
, you could detach the session, and re-attach it in another terminal.
When you detached from previous session, you can safely close the terminal; later use tmux attach
to get back to the session, even if you logged out.
If you just want to pause it and restart afterwards, you can use kill
with STOP
or CONT
signal.
At first find out the processes PID with
$ ps aux
Then send the signals to that PID listed to the process
$ kill -STOP <PID>
$ kill -CONT <PID>
“injcode” from ThomasHabets seems to be exactly the thing I need:
https://github.com/ThomasHabets/injcode
The injcode program allows arbitrary code to be injected into a running
process, whether or not you knew beforehand and were running screen or tmux
From the README:
Example 1: move irssi from one terminal to another
Maybe move it into a screen.
First start irssi in one terminal.
Run injcode in another terminal:
$ injcode -m rettyIrssi should now be moved to the second terminal, including having a new
controlling terminal.
This worked for me:
bg
the processjobs -l
find process numbertmux
start shell window managerreptyr -L PROCESSNUMBER
reptyr
‘s -L
was necessary to get this to work:
-L Like '-l', but also redirect the child's stdio to the slave.
because of this error:
$ reptyr 30622
[-] Unable to open the tty in the child.
Unable to attach to pid 30622: Permission denied
And with -L
$ reptyr -L 30622
Opened a new pty: /dev/pts/4