Bash: Getting PID of daemonized screen session

hexacyanide picture hexacyanide · Jul 4, 2012 · Viewed 16.6k times · Source

If I start a GNU screen session as a daemon, how would I retrieve its PID programmatically? I don't know how consistent the output of screen -ls is, so I'd like to know how to do this with one of bash's constants, $$, $! or a better alternative.

I am starting the screen with screen -dmS screenname.

How would I get the PID of a screen right before or immediately after starting the screen session?

Answer

sarnold picture sarnold · Jul 4, 2012

This show the pid for a screen named 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
$