AtomicPorkchop
AtomicPorkchop

Reputation: 2675

Is there a way to get screen to return a session id or pid?

Is there a way to get screen to echo back the session id when it creates a new window?

I am working on script in perl and I need screen to return the session id or the PID to me so I record it in an array or a hash.

Upvotes: 2

Views: 1428

Answers (2)

Ilion
Ilion

Reputation: 6872

What is your purpose for gathering these pids? It can be a little tricky in perl. Something like Unix::PID might help ( http://metacpan.org/pod/Unix::PID ) but I have the suspicion that your question does not address the actual problem you are trying to solve.

Since you are using screen -dmS <somename> you can do this:

my %screens;

for( $i = 0; $i < 10; $i++) {
  system("screen -dmS server$i");
}

open(my $fh, "screen -list|");
while (<$fh>){
  if (/Detached/) {
    /\s*(\d*)\.(.*?)\s/;
    my ($pid, $name) = ($1, $2);
    $screens{$name} = $pid;
  }
};

Upvotes: 2

Alnitak
Alnitak

Reputation: 339816

Check for the environment variable $ENV{'STY'} within any programs running inside screen.

On my MacOS X 10.6 system at least, it contains the session ID, e.g.:

29379.ttys000.hostname

and where the first field is the PID.

From outside screen, you can run:

screen -list

to get a list of all of your sessions.

Failing that, it's unclear how you're actually starting screen from within your script, but if you use a standard fork / exec model then the child PID available after the call to fork will be the required PID. See man perlipc for more details on how to fork a child program and interact with it.

Upvotes: 1

Related Questions