Moving a Running Process to screen
I am not sure how many times this has happened to me, but it’s not infrequent. I’m working on a remote session and I start a long running job. Then some time later I want to disconnect from the session but realise that if I do then the job will be killed.
I should have started job in
So, is it possible to transfer the running process to
screen? (Or, equally, to
tmux?) Well it turns out that it is using the
reptyr utility. I discovered this thanks to a LinkedIn post by Bruce Werdschinski. A slightly refinement of his process is documented below.
Create a Long Running Job
For illustration purposes, let’s kick off a long running job.
That should start logging text to the terminal.
Find the Process ID
Now we need to find out the PID for that process.
- Suspend the process using Ctrl-z.
- Find the PID using
Right, so the PID is 20562.
Transfer Process to screen
At this point we need to get around a small wrinkle, circumventing a minor security measure.
- Start a
reptyrto reparent the process.
The suspended process will be resumed.
- Disconnect from the
You can update the
ptrace settings permanently, but given that reparenting should not be a frequent process, this is probably not necessary.