How to terminate screen terminal sessions cleanly on macOS

So how do you communicate with a serial device that is connected to your Mac?

In this situation, you can use the screen command on your Mac to do so.

After you terminate the serial session with Ctrl+A+D, you may find that the serial device is still being hogged by the screen command.

When you try to access the device again, you may encounter a Resource busy error message.

For example, after I detached my screen terminal from /dev/cu.SLAB_USBtoUART, I will get the following message if I try to connect to the same device again:

Cannot open line '/dev/cu.SLAB_USBtoUART' for R/W: Resource busy

Given these points, this post shows how we can terminate screen terminal sessions cleanly on macOS.

Steps to terminate screen terminal sessions cleanly on macOS

In order to create the situation of resource hogging, let's connect your serial device with the screen command. For example, I may connect to my ESPx device with the following command:

screen -port /dev/cu.SLAB_USBtoUART 115200

After doing so, detach the session by pressing Ctrl+A+D.

When you do so, the serial device is still being locked by the screen command.

Given that, let's list the screens that are being locked with the following command:

screen -list

After the command completes, you should see something like this the following as an output:

There is a screen on:
	10184.ttys001.ZZZM56582454A	(Detached)
1 Socket in /var/folders/vs/lx78cw0x7m921p6qdmw9h4xm0000gr/T/.screen.

In order to terminate the screen terminal cleanly, run the following command:

screen -X -S 10184 quit

After the command completes, you can run the screen command to verify if the session had been terminated:

screen -list

When the previous terminal session had been terminated successfully, you should see the following output:

No Sockets found in /var/folders/vs/lx78cw0x7m921p6qdmw9h4xm0000gr/T/.screen.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.