Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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)?

share|improve this question
1  
screen or DISPLAY? –  Oli Aug 9 '13 at 18:54
add comment

2 Answers

up vote 1 down vote accepted

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.
share|improve this answer
add comment

Do you mean the screen program? screen -ls will list screen processes along with their screen name, prepended by the PID they are running from:

screen -S foo
screen -ls

There are screens on:
    8806.foo        (09/08/13 20:05:22)     (Attached)

You can use that to kill the process:

kill -15 $(screen -ls | grep '[0-9]*\.foo' | sed -E 's/\s+([0-9]+)\..*/\1/')

Alternatively, if you can identify the php process with ps, then it's parent id will be screen and you can kill that. pgrep -U <myusername> -f <name> will help find the php process you want to find (note the -f which searches the command arguments as well as the command name). You may be running more than one php script, so -f will be a better mechanism to match your process. <myusername> would be your username, <name> would be a string to match the process by. Then you can use ps -p <pid> -o ppid= to get the screen process ID and kill that.

kill -15 $( ps -p $(pgrep -U fooman -f foobar_process) -o ppid= ) 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.