Bash: getting the PID of a demonized screen session

If I started a GNU screen session as a daemon, how would I programmatically get its PID? I do not know how consistent conclusion screen -ls, therefore, I would like to know how to do it using one of the constants of the bash, $$, $!or a better alternative.

I start the screen with screen -dmS screenname.

How do I get the screen PID just before or right after starting a screen session?

+5
source share
6 answers

Shows a pid for a screen with the name nameofscreen:

$ screen -ls
There are screens on:
    19898.otherscreen   (07/03/2012 05:50:45 PM)    (Detached)
    19841.nameofscreen  (07/03/2012 05:50:23 PM)    (Detached)
2 Sockets in /var/run/screen/S-sarnold.

$ screen -ls | awk '/\.nameofscreen\t/ {print strtonum($1)}'
19841
$ 
+12
source

, , PID , , , . ( , - !)

pgrep , PPID PID . - :

rm mypidfile
screen -dmS blah sh -c 'echo $$ > mypidfile ; exec sh'
# the write to mypidfile is happening in the background, so wait it to show up
while [ ! -s mypidfile ]; do sleep 1; done
pid=`cat mypidfile`
# $pid is now the PID of the shell that was exec'ed inside screen
+2

:

screen -DmS nameofscreen

, pid.

-ls , . , :

, :

fess@hostname-1065% screen -ls
There is a screen on:
        19180.nameofscreen    (01/15/2013 10:11:02 AM)        (Detached)

, -D -m -d -m, . pid. ( posix)

fess@hostname-1066% screen -DmS nameofscreen & 
[3] 19431
fess@hostname-1067% pid=$! 

:

fess@hostname-1068% screen -ls
There are screens on:
        19431.nameofscreen    (01/15/2013 10:53:31 AM)        (Detached)
        19180.nameofscreen    (01/15/2013 10:11:02 AM)        (Detached)

:

fess@hostname-1069% echo $pid
19431

:

fess@hostname-1070% screen -S $pid.nameofscreen -X quit
[3]  - done       screen -DmS nameofscreen

:

fess@hostname-1071% screen -ls 
There is a screen on:
        19180.nameofscreen    (01/15/2013 10:11:02 AM)        (Detached)
+2

PID :

$ screen -ls
There are screens on:
        1934.foo_Server         (01/25/15 15:26:01)     (Detached)
        1876.foo_Webserver      (01/25/15 15:25:37)     (Detached)
        1814.foo_Monitor        (01/25/15 15:25:13)     (Detached)
3 Sockets in /var/run/screen/S-ubuntu.

, , PID , Bash, foo_Monitor. PID foo_Monitor, PID bash, , PPID ( PID) PID:

$ ps -el | grep 1814 | grep bash
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  1815  1814  0  80   0 -  5520 wait   pts/1    00:00:00 bash

PID bash:

$ ps -el | grep 1814 | grep bash | awk '{print $4}'
1815

PID. , -v grep bash, , bash:

echo $(ps -el | grep $(ps -el | grep 1814 | grep bash | awk '{print $4}') | grep -v bash | awk '{print $4}')
23869

1814 PID :

echo $(ps -el | grep $(ps -el | grep SCREEN_SESSION_PID | grep bash | awk '{print $4}') | grep -v bash | awk '{print $4}')
+2

- screen -Q :

screen -S nameofscreen -Q echo '$PID'

Please note that this also displays the PID inside the screen session as a notification.

0
source

To execute the sarnold answer:

$ screen -ls
There are screens on:
    19898.otherscreen   (07/03/2012 05:50:45 PM)    (Detached)
    19841.nameofscreen  (07/03/2012 05:50:23 PM)    (Detached)
2 Sockets in /var/run/screen/S-sarnold.

$ screen -ls | awk '/\.nameofscreen\t/ {print strtonum($1)}'
19841

... get the PID of processes with this PID as PPID as follows:

$ ps --ppid 19841 -o pid=
19842
0
source

All Articles