Ssh – Find out screen id

gnu-screenPHPsshUbuntu

I want to start a program with SSH using PHP, which works fine, but now I want to kill the screen with PHP, but the only thing I know is the name of the screen. How do I find out the screen ID (automatically)?

Best Answer

You can use the environment variable $STY to determine whether you're in a screen session and also what the name of the session is that you're inside.

Example

Initially we're sitting in a terminal window, not inside of a screen session.

$ echo $STY

$

Spin up a screen session:

$ screen -ls
There is a screen on:
    31543.tscrn (Detached)
1 Socket in /var/run/screen/S-saml.

Connect to it:

$ screen -r 31543.tscrn

Inside screen session:

$ echo $STY
31543.tscrn
$

Killing a session

With the name of the session you can kill it using screen.

$ screen -X -S tscrn kill

You can also use the number there too.

$ screen -X -S 31543 kill

Confirm that its been killed:

$ screen -ls
No Sockets found in /var/run/screen/S-saml.
Related Question