Kill detached screen session
#1
I've been attempting to get rid of a detached screen session in a Linux environment. I'm aware that `screen` is a highly valuable tool for maintaining long-running processes, but sometimes it's necessary to terminate these sessions. I came across the command `screen -X -S [session #] kill` which, theoretically, should kill the specified session. I first list the available screen sessions using:


This command returns a list of running screen sessions. For example:

Code:
12345. pts - 1. devhost(Detached)
23456. pts - 2. devhost(Detached)
2 Sockets in /var/run / screen / S - yourusername.

To kill a session, I'm replacing `[session #]` with the actual screen session number that I want to terminate, like so:


Unfortunately, this method does not seem to work. The screen session persists even after running the command. Is there anything incorrect with the syntax or the method I'm using? Is there a more reliable way to end a detached screen session?
Reply
#2
The command you're using looks correct at first glance. Make sure you're using the right session number after `-S`. If the session name contains dots or special characters, you might have to escape them or use quotations. Try the command with quotes around the session ID, like this:

Code:
kill

Make sure you have the necessary permissions to kill the screen session, as attempting to kill a session owned by another user will fail unless you're using superuser privileges.
Reply
#3
I think code_mastermind has pointed out a potential issue with special characters in the session name. However, if that still doesn't resolve your problem, you could simplify the process by using the `-r` option to reattach and then exit. Here’s how you can do that:
Firstly, reattach to the session with:


Once inside the session, you can simply exit by typing `exit` or pressing `Ctrl-D`. Alternatively, you can issue a kill command from inside the screen session:

Code:
kill - 9 $$

This should terminate the session. However, if you really want to kill the session from outside, and the previous suggestions didn't work, you could use `screen -ls` to get the list of sessions and then pipe it to `awk` and `xargs` to kill all sessions like this:


This snippet will forcefully terminate all detached sessions. Use with caution, as it does not discriminate between sessions.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)