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?
To get the same functionality as explained in the post with the most votes you would need to do the following:
You can now safely logoff from the remote machine, your process will keep running inside
If you want to have multiple session running side-by-side you should name each session using Ctrl-B and $. You can get a list of the currently running sessions using
You could do that by using
Screen and nohup is the better way, but if you have to detach a process already running without screen or nohup you can run disown command. With disown you can close the terminal and get the process running on the machine.
You cannot do this once the process has started, you need to have set things up before you run a long running job.
You can use nohup but modern wisdom suggests you use screen or byobu as your login so you can detach and leave things running.
Screen has the advantage that you can detach from one machine and reattach from another which is handy if you want to check on long running processes that run beyond the end of the working day.
There is a reasonable getting started guide to screen here.
byobu puts an easy to use interface on top of screen with menus etc. It's also the current implementation of screen on newer ubuntu. F2 to start a new terminal F3/F4 to toggle back and forth and F6 to disconnect. Type exit to actually end terminals permanently.
There are two major programs you can use to maintain programs and terminal state over multiple ssh connections. They are screen (the incumbent, but unfortunately unmaintained. Apparently being actively developed now) and tmux (newer, actively maintained). Byobu is a front end that can run on top of their of these systems and offer additional ubuntu status information. On new installations it will use tmux as a backend, if you have an older installation of byobu and an existing config it will maintain the previous backend, be it screen or tmux.
Byobu can be installed on the computer by doing so in a Debian-based machine:
Using yum, you do
It's also possible to install byobu on other distributions.
You can start byobu by running
You can also use Byobu Terminal on a Ubuntu machine with -X option and easily have a perfectly working byobu.
Start byobu by typing
You can press F2 to create a new window within the current session, F3-F4 to switch between the various windows.
The best part about byobu is, you dont have to actually kill the processes running in the terminal to leave the terminal. You can simply send screen/tmux (the skeleton of byobu) to background and resume the next time you come:
You can also create various byobu sessions by
The best way is often the simplest.
It was made specifically for this, it even logs stdout to nohup.log
If you want to "background" some already running tasks, then your best bet is to ctrl+z then run
then a quick
Keep in mind, both are bash specific. If your not using bash, then the commands could be different.
Hey, while I agreed that screen is the most efective option. You can use vncserver and then start the process on it.
Also if your only interes is to have the process running and no need to take control back of it, and utterly most important you were not aware you will need to close the session and you have the process already running, you are not of luck if you used bash as the shell
First you need to send the process to background by typing Ctrl+Z followed by bg %1 (the number depends on the job number, usually it is 1, but you can easily pull the list using the command jobs)
Finally invoke the command disown (followed by the jobid ... same as with bg command)
This will remove the parent-child relationship between your shell and the process in background, preventing it to die when your shell is terminated.
You should check out GNU Screen and see if it helps you. Depending on how you need you application to run in realtime, it may cause more issues than it solves, but at least it will allow you to resume your session as if you never left it.
How to use :
There are many other options, for example split screens, and also all shortcuts are fully customizable.
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:
Step 3 pauses the current process (e.g. my 'mv' command). Step 4 puts the paused process in to the background and resumes it. Step 5 lets you disown the process.
For a single shell script that I have running over a long period of time, I will login, and run the process in the background using '&'.
I've logged out and disconnected my SSH session. When I log in some time later, the script is still executing as proven by continuous data collection from the script.
ctrl+z will suspend the running program
"bg" will run it in the background