Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to keep processes running after ending ssh session?

Let's say I launch a bunch of processes from a ssh session. Is it possible to terminate the ssh session while keeping those processes running on the remote machine?
by

3 Answers

akshay1995
Option 1: nohup
The best way is often the simplest.

nohup long-running-command &

It was made specifically for this, it even logs stdout to nohup.log.

man nohup

Option 2: bg + disown

ctrl+z
bg
disown -h
RoliMishra
You could do that by using screen.

Type man screen to find out more or read this screen man page.

Simple scenario:

ssh into your remote box. Type screen Then start the process you want.

Press Ctrl-A then Ctrl-D. This will "detach" your screen session but leave your processes running. You can now log out of the remote box.

If you want to come back later, log on again and type screen -r This will "resume" your screen session, and you can see the output of your process.
pankajshivnani123
I was stuck in a large mv so I wasn't in a position to stop the process, setup screen and then start it again. I managed to exit the SSH session with the process running by essentially doing the following steps:

Establish SSH connection: ssh user@host
Run the desired command to start the process
Press Ctrl+Z to pause the process
Run bg to put the paused process in the background and resume it.
Run disown [pid] (process ID is optional, defaults to last process) to disown the process. To get a list of jobs simply type jobs before.
Exit the SSH session by running logout.
Usage of the disown command:

disown [-ar] [-h] [jobspec ... | pid ... ]
Without options, remove each jobspec from the table of active
jobs. If jobspec is not present, and neither the -a nor the -r
option is supplied, the current job is used. If the -h option
is given, each jobspec is not removed from the table, but is
marked so that SIGHUP is not sent to the job if the shell
receives a SIGHUP. If no jobspec is supplied, the -a option
means to remove or mark all jobs; the -r option without a job?
spec argument restricts operation to running jobs. The return
value is 0 unless a jobspec does not specify a valid job.

Login / Signup to Answer the Question.