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)?
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.
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= )
screen
orDISPLAY
? – Oli Aug 9 '13 at 18:54