Categories: Ubuntu

How to kill all detached screen sessions on Linux or Unix

In this article, we will see how to kill all detached screen session on Linux or Unix based systems. Screen utility provides a powerful feature of managing task and processes by detaching the session and allow it to run on background while you can perform other activities on foreground. This is mainly useful when you have to perform a long running task on remote servers such as taking database backup where you don’t want the session to disconnect in between. Detaching session is a very flexible feature that allows you to perform multiple task from a single terminal interface.

You can always reattach the session to terminal and continue with your task. However sometimes you might face a scenario where you would like to kill all detached sessions running on the background. Here we will see how you can kill all the detached sessions on Linux or Unix based systems.

 

How to kill all detached screen sessions on Linux or Unix

Also Read: How to Install PowerShell on Ubuntu or Debian

Before looking into how to kill detached sessions, let’s first create few sessions.

cyberithub@ubuntu:~$ screen -d -m top
cyberithub@ubuntu:~$ screen -d -m watch ps -ef

As you can see above, we are creating two screen sessions. Below are the options used with above screen commands:-

  • -d: This option tells Screen to start a session in “detached” mode. Normally, when you start a Screen session, it opens and attaches to your current terminal window.
  • -m: This option forces Screen to create a new session, even if Screen is called from another Screen session. It ensures that a new session is created in all circumstances.

 

 

List all running sessions

To list all running screen sessions, run screen -ls command as shown below.

cyberithub@ubuntu:~$ screen -ls
There are screens on:
182568..ubuntu (23/03/24 06:16:37 PM IST) (Detached)
179434..ubuntu (23/03/24 06:15:39 PM IST) (Detached)
2 Sockets in /run/screen/S-cyberithub.

 

List only detached sessions

To list only the detached screen sessions, run screen -ls | grep Detached command as shown below.

cyberithub@ubuntu:~$ screen -ls | grep Detached
182568..ubuntu (23/03/24 06:16:37 PM IST) (Detached)
179434..ubuntu (23/03/24 06:15:39 PM IST) (Detached)

 

 

Kill a detached session

To kill a detached session, run screen -S -X quit command. For example, in our case we would like to kill a session with id 182568, so in that case we have to run screen -S 182568 -X quit command as shown below.

cyberithub@ubuntu:~$ screen -S 182568 -X quit

Here are the details of the options used with above screen command:-

  • -S: This option is used to specify the session name or session id.
  • -X: This option allows you to send a command to a specific screen session.

 

Reattach & kill a session

Before killing a detached screen session, if you are looking to reattach then run screen -r command. For example here we are reattaching session id 182568 by using screen -r 182568 command.  Here -r option is used to reattach a session. Then killing that session by pressing q. Some of the sessions can also be killed by running exit or Ctrl+c command.

cyberithub@ubuntu:~$ screen -r 182568
[screen is terminating]

 

 

Kill all detached sessions

If you are looking to kill all the detached sessions only, then you can easily do it by using below command.

cyberithub@ubuntu:~$ screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill &> /dev/null

Now if you check again, you will see there are no active screen sessions running.

cyberithub@ubuntu:~$ screen -ls
No Sockets found in /run/screen/S-cyberithub.
Ubuntu Server Admin

Recent Posts

Advantech AOM-2721 is now Ubuntu Certified

Canonical announces that the Advantech AOM-2721 is officially joining the list of Ubuntu Certified Hardware.…

9 hours ago

Ruby and Rails Performance Roundup: The Backlog Edition

Yet again instead of tweets, a blog post. The backlog got out of hand -…

9 hours ago

How to Deploy OpenProject on Ubuntu VPS

This article provides a guide demonstrating how to deploy OpenProject on Ubuntu VPS. What is…

9 hours ago

Canonical integrates NVIDIA Nemotron 3.5 Lightning with Ubuntu for always-on AI agents

Canonical is pleased to announce that  NVIDIA’s newly introduced NVIDIA Nemotron 3.5 Lightning, an open,…

2 days ago

Xfwl4’s Second Preview Release

I’m pleased to announce the second preview release of xfwl4, Xfce’s Wayland compositor, version 4.21.1.…

2 days ago

How to Install, Secure, and Set Up MySQL on Ubuntu 26.04 LTS

If you have been using an ecommerce store to place online orders or have been…

3 days ago