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.
For illustration purposes, let’s kick off a long running job.
$ tail -f /var/log/syslog
That should start logging text to the terminal.
Now we need to find out the PID for that process.
- Suspend the process using Ctrl-z.
+ Stopped tail -f /var/log/syslog
- Find the PID using
$ jobs -l + 20562 Stopped tail -f /var/log/syslog
- Right, so the PID is 20562. At this point we need to get around a small wrinkle, circumventing a minor security measure. Enable
$ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
- Start a
reptyrto reparent the process.
$ reptyr 20562
- The suspended process will have resumed. Disconnect from the
$ echo 1 | sudo tee /proc/sys/kernel/yama/ptrace_scope
You can update the
ptrace settings permanently, but given that reparenting should not be a frequent process, this is probably not necessary.