Tuesday, 23 April 2013

Screen: how to run processes in the background after log out

So, you want to run a very long process on your ssh server but you need to log out.

It sounds like an impossible problem, but as long the computer is on, why can't it run processes in the background? Well, it can, and there is a simple solution using a quite clever command-line window manager:

Screen

Screen can create multiple windows in a shell, from which you can "detach" and leave them running in the background, even if you log out of your user account.


Create a Screen


From a terminal, type:

$ screen -S [choose-a-window-name]

And you will immediately enter the window just created.
From this window you can type any command and leave it running in the background.

To detach from the window and go back to the terminal, just press the combination:

ctrl-a d

And you will go back to the terminal, where you can start more screens if needed, or even log out without stopping the job.


Access a previously created Screen


From a terminal, type:

$ screen -r [window-name]

to revert a previously created screen. If you do not remember the window name, you can list them through the command:

$ screen -ls

which gives information about the active screens and their names.


Kill a Screen


If your job is finished in one screen and you want to close it, you can go revert it and press:

ctrl-d

to close it, or you can close it from outside the screen through the command:

$ screen -X -S [window-name] kill


Conclusion


Screen is a very powerful tool and its uses span a much wider scope than the ones described here. It can be regarded as complete full-screen window manager.

For ssh users, though, it is a lifesaver as it allows to run for nasty and long processes at anytime of the day without needing your presence.

No comments:

Post a Comment