Being able to run your commands and scripts directly from the command-line of your chosen machine type is one of the biggest advantages of cloud computing. However, some commands or scripts may need to run for several hours, or even several days without interruption. If such processes are submitted in the standard terminal, any interruptions to the terminal connection (due to accidentally closing the terminal, or unexpected internet dropouts etc) will result in your analysis being terminated prematurely. Fortunately, there is a simple solution with a helpful little program known as GNU screen.

GNU Screen allows you to create and manage one or several terminal sessions that enables commands to be run separately from the main terminal and to continue running in the background even when the terminal is disconnected. Below we present a quick guide for getting started with GNU Screen and some of the most helpful options that will ensure you never have to worry about prematurely terminating your analyses again.

Installing GNU Screen

If you work with Debian based distributions, GNU Screen is already available on every Ubuntu machine that is launched with RONIN, so no installation is necessary.

If you work with Red Hat, you can easily install GNU Screen with the following command:

sudo yum install screen

Launching a new screen session

To launch a new screen session, just run the screen command:

screen

This will open a fresh terminal session where you can start running your commands and scripts.

If you plan on running multiple commands/scripts at the same time, it's sometimes handy to name your screen sessions to keep a track of them. This can easily be done by using the -S flag when launching a new screen session.

screen -S name

Detaching from a screen session

Once you have started running your command or script in a screen session, you can disconnect from the session and go back to the main terminal by pressing "Ctrl a" followed by "d" (for detach).

All analyses running in the screen session will continue in the background even when you disconnect from the main terminal session. At any time you can check back and see what's happening or where your analysis is up to by reattaching to the same session (see below).

Reattaching to a screen session

If you only have a single screen session running you can reconnect to that screen at any time using the -r flag (for reattach):

screen -r

If you have multiple screen sessions you can list the available sessions using the ls screen command:

screen ls

You can then connect to the desired screen by adding the screen session ID (the numbers prior to the first . ) or the name you gave the screen session (using the -S flag) to the reattach command e.g.:

screen -r 517894
screen -r name

Logging a screen session

Sometimes you may want any commands and respective output or errors that are printed to the screen during the running of your command/script to be stored in a log file. This can be achieved by adding the -L flag when commencing a screen session:

screen -L

This will create a log file with a default name such as "screenlog.0" but you can specify the name of the log file yourself using the -Logfile flag:

screen -L -Logfile mylog.txt

If you forgot to include the -L flag when starting your screen session, never fear as you can start logging at any time from within the terminal session by pressing "Ctrl a" followed by "H". Note the H must be a capital!

Scrolling through screen sessions

You will notice that when you are in a screen session you cannot scroll through the window as usual to see previous commands or logs. If you wish to scroll back through your screen session you will need to enter scrollback mode by pressing "Ctrl a" followed by "Esc". You can then press "Esc" again when you are done to get back to using the terminal.

Terminating a screen session

Once you have finished with your analysis and no longer need your screen session, you should first reconnect to the session and then terminate the session by pressing "Ctrl a" followed by "k" (for kill) and then hit "y" to confirm.

We've covered pretty much all the basics for getting started with GNU Screen here, but of course there are a ton of other options/features which you can explore using the screen -help command or by pressing "Ctrl a" followed by "?" in one of your screen sessions.

Now it's time to sit back and relax while your analyses run peacefully in the background.