Ruan Bekker's Blog

From a Curious mind to Posts on Github

Managing Background Processes With Screen

image

This is a quick post on how to create, manage and delete background processes with screen

About

Screen allows you to run processes in a different session, so when you exit your terminal the process will still be running.

Install

Install screen on the operating system of choice, for debian based systems it will be:

1
$ sudo apt install screen -y

Working with Screen

To create a screen session, you can just run screen or you can provide an argument to provide a name:

1
$ screen -S my-screen-session

Now you will be dropped into a screen session, run a ping:

1
$ ping 8.8.8.8

Now to allow the ping process to run in the background, send the commands to detach the screen session:

1
Ctrl + a, then press d

To view the screen session:

1
2
3
4
$ screen -ls
There is a screen on:
  45916.my-screen-session (Detached)
1 Socket in /var/folders/jr/dld7mjhn0sx6881xs_0s7rtc0000gn/T/.screen.

To resume the screen session, pass the screen id or screen name as a argument:

1
2
3
$ screen -r my-screen-session
64 bytes from 8.8.8.8: icmp_seq=297 ttl=55 time=7.845 ms
64 bytes from 8.8.8.8: icmp_seq=298 ttl=55 time=6.339 ms

Scripting

To use a one liner to send a process as a detached screen session for scripting as an example, you can do that like this:

1
$ screen -S ping-process -m -d sh -c "ping 8.8.8.8"

Listing the screen session:

1
2
3
$ screen -ls
There is a screen on:
  46051.ping-process  (Detached)

Terminating the screen session:

1
$ screen -S ping-process -X quit

Thank You

Let me know what you think. If you liked my content, feel free to visit me at ruan.dev or follow me on twitter at @ruanbekker

ko-fi