I'm connecting to a linux machine through SSH, because I'm trying to run a heavy bash script that makes filesystem operations, and it's expected to keep running for hours. But I cannot leave the SSH session open because of internet connections issues I have. I doubt that running the script with the background operator, the ampersand &, will do the trick, because I tried it and later found that process was not completed. How can I logout and keep the process running?
|
The best method is to use screen. Screen is a virtual terminal which you can run from a "real" terminal (actually all terminals today are virtual). Screen will keep running even if your ssh session gets disconnected. Any programm which you start from screen will keep running in that screen session. When you reconnect the ssh session, you can reconnect to the screen session and everything will be as if nothing happened, other than the time which passed. The disadvantage of screen is the steep learning curve. Here is a nice tutorial with diagrams explaining the logical structure. An alternative to screen is tmux. Another method is to use nohup. Programs started with
As an alternative to |
|||||||||||||||||||
|
|
There are a few ways to do this, but the one I find most useful is to use GNU Screen. After you ssh in, run This will "disconnect" you from the screen session. At this point, you can log out or do anything else you'd like. When you want to re-connect to the screen session, just run |
|||||||||||||
|
|
In
By typing
If you were to log out at this point, the background task would also get killed. However, if you run
You can confirm this:
You can even combine the & and the disown on the one line, like so:
Better than running |
|||||||||||||||
|
|
The tool nohup, available on most Linux boxes will do this. |
||||
|
Just to be thorough, I'll point out tmux, which has the same basic idea as screen:
It is, however, approximately infinitely easier to search for on Google. |
|||
|
Screen is overkill for just keeping processes running when you logout. Try dtach:
|
|||||
|
|
"byobu" on Ubuntu is a nice front-end to screen, by pressing "Ctrl-?" you get a list of all the keyboard shortcuts. It adds a status bar that can be useful for watching CPU load, disk space, etc. Overall it provides an experience that I would describe as a terminal based VNC connection. "nohup" allows starting a job in the background with it's output routed to a log file, which can always be redirected to /dev/null if not required. |
|||
|
|
|
The
And you can then enter a command or series of commands that will be run. The results should be e-mailed to you, if e-mail is set up correctly on the machine. Instead of |
|||
|
|
|
Here's a way to daemonize any shell process, no external programs needed:
When you then close your session, the job will continue to run as evidenced by the output.txt file (which has buffering so it takes a while to show non-zero). Don't forget to kill your job after testing :-) So all you need to do is close stdin and background the job. To be really good, first cd to / so you don't hold on to a mount. This works even in simple sh under Solaris. |
|||||||||||||||
|