DEV Community

Sayem Hoque
Sayem Hoque

Posted on

How to use GNU screen to run processes

Screen is an ancient but still useful software on the GNU operating system that's available on most linux distros and on Macs.

For example, if you need to SSH to a remote server and need to run a long process that and can't worry about whether the connection will terminate, running the process in a screen will run your process in the background such that even if a connection is lost, it'll keep operating. When you reconnect to the screen, the state of the session will be as-is when you exited.

Steps

screen

sh@SH-MacBook-Pro tutorial % screen
Enter fullscreen mode Exit fullscreen mode

you should see a start message here, which you can just press enter to get past.

You're now 'attached' to the screen session. To see that you're attached, you can see a list of screens and their statuses by running screen -ls:

sh@SH-MacBook-Pro tutorial % screen -ls
There are screens on:
        69475.ttys010.SH-MacBook-Pro      (Attached)
1 Socket in /var/folders/vw/865hm_fd2pj87qtwf_5n329m0000gn/T/.screen.
Enter fullscreen mode Exit fullscreen mode

While you're attached, you can run a script and have it start processing whatever it is that you're doing.

sh@SH-MacBook-Pro tutorial % python somescript.py
             >> This is some logging being produced by
             >> a script that you're running
Enter fullscreen mode Exit fullscreen mode

As your process is producing output, instead of hitting Ctrl+c, you can hit Ctrl + a + d to "detach" from the screen. The process will keep running in your still-attached screen session. You'll now be back in your normal terminal, and see a log that says you just detached from a screen:

[detached]
sh@SH-MacBook-Pro tutorial %
Enter fullscreen mode Exit fullscreen mode

If you run screen -ls again here, you'll see that there is an active screen that you're detached from.

sh@SH-MacBook-Pro tutorial % screen -ls
There is a screen on:
    68979.ttys002.SH-MacBook-Pro    (Detached)
1 Socket in /var/folders/vw/865hm_fd2pj87qtwf_5n329m0000gn/T/.screen.
Enter fullscreen mode Exit fullscreen mode

To reconnect to your screen, you can run screen -r. If you only have one active screen, you can just run screen -r. If you have multiple active screens to choose from, run screen -r with the screen id (68979 in this case).

sh@SH-MacBook-Pro tutorial % screen -r 68979
Enter fullscreen mode Exit fullscreen mode

This will take you to your screen session that you created before.

sh@SH-MacBook-Pro tutorial % python somescript.py
             >> This is some logging being produced by
             >> a script that you're running
             >> More stuff was logged since we last checked
Enter fullscreen mode Exit fullscreen mode

To exit a screen, Ctrl + d will exit the session. This kills the current screen session, and you won't be able to re-attach to it.

Scrolling

Within a screen session, scrolling up the terminal window with the mouse will not work as usual.

Scroll up a screen you're attached to by entering "scrollback" mode.

Ctrl + a + Escape will enter you into scrollback mode. Once you're in here, arrow keys will allow you to actually go up the screen log history. Mouse still won't work sorry.

Top comments (0)