Move Long Running Linux Process To Screen Session

Have a long running Linux process open in a SSH terminal window and need to shutdown your computer without killing it? I run into this on a regular basis and never spent the time looking into a solution but this past weekend I actually decided to look and came up with a cool solution called reptyr. The reptyr application allows you to open screen and migrate the process to the screen terminal and away from the terminal connection opened without screen. This allows you to detach from the window without killing the process. Below is a quick overview of installing reptyr and how to use it.

**NOTE: You should test migrating the process as I have seen a couple times where the process is killed during the migration process though I believe this happens when the process you are running has kicked off a bunch of sub processes via shell script or similar.

Install reptyr To Provide The Ability To Migrate Processes To Different Terminal:

First you need to download and install reptyr which can be done by using git as shown in the below example output. Below is how to clone the git project followed by a listing of the directory showing the new reptyr directory created by the git clone process.

Now that you have pulled the reptyr source down you can compile and install it using the below output as an example. The examples above and below were all completed on CentOS Linux but should be similar on any Linux distribution.

Compile & Install reptyr On CentOS Linux:

bash

[root@dev reptyr]# make

cc-Wall-Werror -D_GNU_SOURCE -g-c-o reptyr.o reptyr.c

cc-Wall-Werror -D_GNU_SOURCE -g-c-o ptrace.o ptrace.c

cc-Wall-Werror -D_GNU_SOURCE -g-c-o attach.o attach.c

cc reptyr.o ptrace.o attach.o -o reptyr

[root@dev reptyr]# make install

install-d-m755/usr/local/bin/

install-m755 reptyr /usr/local/bin/reptyr

install-d-m755/usr/local/share/man/man1

install-m644 reptyr.1 /usr/local/share/man/man1/reptyr.1

install-d-m755/usr/local/share/man/fr/man1

install-m644 reptyr.fr.1 /usr/local/share/man/fr/man1/reptyr.1

[root@dev reptyr]#

With the above commands out of the way you are now ready to use reptyr to attempt to migrate a process. To do this you must obtain the PID or Process ID of the process you want to migrate and then migrate it using the below command. Again please be careful attempting to do this on critical processes without first testing because if the process has spun up sub processes it could kill them during the process migration to the new terminal session.

Use reptyr To Migrate A Process On CentOS Linux:

bash

[root@dev reptyr]# reptyr -s 24054

The above process was issued from a screen session and migrated top running in another terminal to the new session running via screen. It worked flawlessly and will definitely be a command I use on a regular basis as I am constantly thinking certain commands will take less time than they do and getting caught at the office not wanting to stop the process. If you are not sure how to obtain a PID (Process ID) you can use “ps -ef” piped to grep to filter the processes running on a Linux box.