Debian

How to Kill a Screen Session in Linux?

How to kill a screen session

Sessions on Linux are the shells that allow to interact the user with the system, thus acting as a bridge between the user and the kernel of the system. In the shell, there are multiple processes that are running and are referred to as a process group. In Linux screen sessions are a sort of multiplexer terminals as users can open up multiple terminal sessions which aids in multitasking. Moreover, these terminals can be opened in a single terminal window and can be detached depending on the utility.

The processes in the screen window will continue to run in the background even if the window is closed, which can consume a lot of system resources, thus leading to system instability. Therefore, it is necessary to kill the screen session once you have closed it.

How to Kill a Screen Session

Every process on Linux has its process ID through which it can be identified and can be used to terminate the respective process. Moreover, like the process ID, there is a session ID for each session running on Linux and there are numerous ways to kill the screen session:

Method 1: Through Kill Command

The kill command is the built-in shell in Linux which is used to terminate the processes and sessions using their respective IDs. This command just sends the signal to process based on the options used with this command. By default, this command takes the -15 option if not specified, that is the signal of termination of the process. To kill the screen session on Linux using this kill command, here are some steps that should be followed:

Step 1: Get the session ID

To terminate or end any screen session on Linux, it is necessary to get the session ID for the respective session and for that, the following command is used:

ps -ef | grep <screen-session-name>

Here,

  • The ps is for displaying the information of running screen sessions.
  • The flag e is for listing all the screen sessions either currently running or running in the background.
  • The flag f is used to display the full information for all the processes running.
  • The grep is used for searching the sessions with the given name.

Now for demonstration, I have listed the screen session using the above given command:

ps -ef | grep screen

Step 2: Kill the session using session ID

Once you have got the session ID execute the kill command to end the session process and here is the syntax for it:

Kill [option] <session-id>

Here the option defines the type of signal to be sent to the session and there are three options that can be used with the kill command and those are:

Name of Signal Number of Signal Description
SIGTERM 15 Terminates the session or process, leaving the child processes.
SIGKILL 9 It kills the session or process immediately, along with the child process.
SIGHUP 1 Notifies the serial line drop process.
SIGINT 2 Interprets the process

By default, as mentioned above the option is set to 15 which means the termination of the screen session or process. Now use the above syntax to kill the screen session:

Kill -15 2884

Kill all the Screen Sessions

This method is viable if you want to kill all the screens simultaneously, so just execute the killall command:

killall -15 screen

Method 2: Through screen Command

The primary function of the screen command is to display multiple screens on Linux and can also be used to kill the screen session on Linux. On Linux to kill a screen session there are some steps to be followed to use the screen command for terminating the screen session:

Step 1: Get the Session ID

To kill any screen session, to attach or detach the screens on Linux its ID is required, so in that case the screen command can be used with the ls flag to list down all the running screen sessions:

screen -ls

Step 2: Kill the Screen Session using the screen Command

Once the session ID is known, execute the below-given syntax that uses the screen command to kill the screen session on Linux:

screen -X -S [session ID] quit

Here

  • The X flag is used to execute the screen command for the specified session ID,
  • The S flag specifies the session by the given ID.

So now, using the above-given syntax to kill the screen session, here the session ID for screen1 is 2959:

screen -X -S 2959 quit

Killing a Screen Session Without Attaching and Using its Name

If the screen session is not attached, then after the listing of the screen sessions execute the below syntax:

screen -S <screen-name> -X quit

A screenshot of a computer Description automatically generated

Note: To use the screen command, you have to install it first so for that execute:

sudo apt install screen

Method 3: Through pkill Command

The pkill is another utility that can be used to terminate or kill the screen session on Linux using the session’s full or partial name. It is quite similar to the kill command, here are two steps that are to be followed to kill the screen session using pkill:

Step 1: Get the Session ID

To kill any screen session, to attach or detach the screens on Linux its ID is required, so in that case the screen command can be used with the ls flag to list down all the running screen sessions:

screen -ls

Step 2: Kill the Screen Session Using pkill Command

As in the previous step we listed the screen sessions that displayed their names as well, so now use the pkill command along with the session ID to kill the respective screen session:

pkill -s <sessionID>

So, now for demonstration, I have terminated the screen session having an ID 5140 by using the above syntax:

pkill -s 5140

Method 4: Attach Screen and Exit

On Linux, the screen session can be terminated by just exciting it and for that first the screen session should be attached. So here are the two steps that you need to follow in this regard:

Step 1: Attach the Screen Session

To attach the screen session just use the r flag with the screen command and the screen name, to get the list of attached and detached screens use the ls command:

screen -r <screen-session-name>

Step 2: Exit the Screen Session

Once you have attached the screen session, just execute the exit command and the screen will be terminated:

exit

Conclusion

Screen sessions in Linux provide users with the privilege of multitasking, which saves a lot of time while executing the commands. When a screen session is closed, it is most likely to still run in the background, which can consume unnecessary resources. To kill a screen session on Linux there are four ways: using pkill command, screen command, kill command, killall command, and exiting the attached screen.

Similar Posts