- detaching an already running process on a remote server
- start a process and leave it on the background without
screen
detaching an already running process on a remote server
Suppose you sshed into an instance and began a long-running process but forgot to enter screen
(terminal multiplexer) or another technique, and now want to go home. There are two "easy" ways that I do this:
For both of our techniques, suppose the command you're running is ping google.com
1) sshing again, entering screen
and reparenting the process with reptyr
Explanation of the programs used:
-
screen
: a terminal multiplexer, in other words, you can start session with screen and open any other number of windows (like if you were not using screen). The thing here is that you can exit screen and the processes that were running will remain running, even if the window is not visible or you disconnected. This guide has great examples: https://linuxize.com/post/how-to-use-linux-screen/ -
reptyr
: a program that takes an existing running program, which we'll call it a process, and attaches it to a new terminal. Their README.md is pretty good.
Preconditions:
- Install
reptyr
andscreen
.-
screen
:sudo apt-get install screen
-
reptyr
: install the latest version, 0.7.0, because if otherwise, the flag needed (-T
- steal the entire terminal session of the target) won't be present:
-
wget github.com/nelhage/reptyr/archive/reptyr-0.7.0.zip && unzip reptyr-0.7.0.zip
Steps:
1) ssh into the instance and run ping google.com
2) ssh again in another session and start screen with screen
3) fetch the PID of the process running on the other session: ps aux | grep ping google.com
4) with the PID, 20364, enter the reptyr
folder and run ./reptyr -T 20364
5) exit screen with a Ctrl-A, and Ctrl-D. It will display a [detached]
message.
6) the other session that was running the program with be frozen probably, you can just exit it by closing the terminal.
7) done, if you ssh into the instance again, and do ps aux | grep ping
. You'll see that it is running.
2) suspending the process (Ctrl-Z
), resuming it on the background (bg
) and disowning it (disown
).
This is a technique without the need for any other programs besides the ones present by default in Linux (Ubuntu, in my case). Here we use the commands provided to control jobs on Linux
The drawback of this option is that it has some downtime regarding the process execution. The interval between resuming the process on the background accounts for that.
Steps:
1) ssh into the instance and execute ping google.com
2) suspend the process with Ctrl-Z
, which will create a job an entry on the jobs table. Check out here the difference between a job and a process
3) resume the job with bg
(this will resume the last suspended job and run it on the background). Note that the stdout will still be tied to your session.
4) run disown
, which disowns (remove the job from the jobs list in order for you to be able to exit the session and the job not stop). Notice that resuming the jobs on the background with bg
still sends the stdout to your current session, that's why disown
is in between the pings (this thread explains the reason nicely)
start a process and leave it on the background without screen
suppose you don't have screen
installed nor want to install it, you can also use the default tools to achieve this. Run nohup <command> &
and that'll do the job. Let me break down the command:
-
nohup
will intercept the SIGHUP signal from the controlling terminal. This means that when the controlling terminal is closed, the processes running on the session won't receive this signal and thus will continue executing. -
&
will just leave the process on the background, becausenohup
doesn't do that by default (which doesn't make much sense for me because why would someone run a program on the foreground that does not respond to SIGHUP (?))
Note: in some shells, like zsh, when you try to close the terminal, you'll still get a message you have running jobs
. You can just ignore it and close the terminal, the job will continue running.
Top comments (0)