Published on

Managing Background Processes with Screen

Authors

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:

$ 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:

$ screen -S my-screen-session

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

$ ping 8.8.8.8

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

Ctrl + a, then press d

To view the screen session:

$ 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:

$ 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:

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

Listing the screen session:

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

Terminating the screen session:

$ screen -S ping-process -X quit

Thank You

Thanks for reading, if you like my content, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.

Buy Me A Coffee