LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2009, 01:32 PM   #1
marsupillami
LQ Newbie
 
Registered: Aug 2005
Location: Portugal
Distribution: Fedora Core 2
Posts: 7

Rep: Reputation: 0
Get PID of screen session started as daemon


Hello everyone,

I´m trying to start a screen session from a shell script. Here is how it looks like (simplified part):

Code:
cd /home/teste/14356/
screen -dmS 145363 ./execute.sh           
echo $$
It is returning me a wrong PID (example, it now returned 5205 when screen´s PID actually is 5207). How can I get the correct PID in order to store it and use it for further manipulation?

Thank You,
 
Old 06-08-2009, 03:20 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by marsupillami View Post
Hello everyone,

I´m trying to start a screen session from a shell script. Here is how it looks like (simplified part):

Code:
cd /home/teste/14356/
screen -dmS 145363 ./execute.sh           
echo $$
It is returning me a wrong PID (example, it now returned 5205 when screen´s PID actually is 5207). How can I get the correct PID in order to store it and use it for further manipulation?

Thank You,
What's happening is its returning the PID of the bash shell.

Code:
cd /home/teste/14356/
screen -dmS asdfasdf ./execute.sh
screen -list | grep asdfasdf | cut -f1 -d'.' | sed 's/\W//g'
is kinda dirty but will work... might wanna make it more like:

Code:
#!/bin/bash
cd /home/teste/14356/
UNIQID=`date +%Y%M%d%H%M%S`
screen -dmS $UNIQID ./execute.sh
screen -list | grep $UNIQID | cut -f1 -d'.' | sed 's/\W//g'
You could further refine it by getting a directory and script to execute or taking the id from the commandline, etc.

Last edited by rweaver; 06-08-2009 at 03:22 PM.
 
Old 06-08-2009, 03:37 PM   #3
marsupillami
LQ Newbie
 
Registered: Aug 2005
Location: Portugal
Distribution: Fedora Core 2
Posts: 7

Original Poster
Rep: Reputation: 0
Hello rweaver

That worked perfectly. Thank you very much!
 
Old 06-08-2009, 03:57 PM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by marsupillami View Post
Hello rweaver

That worked perfectly. Thank you very much!
Also of note, you could just do a screen -list and it'll show all the running screens on the server for your user.

Also im not sure that screen command is going to do what you want... just a fyi.

Last edited by rweaver; 06-08-2009 at 04:01 PM.
 
Old 06-08-2009, 05:16 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You seem to know what you're doing, but just in case:

Why do you need the PID of the screen proces(ses)? If you're automating/scripting screen-stuff, you can probably do more with the session name you give the screen session(s) with the -S option than with it's PID.

Last edited by Hko; 06-08-2009 at 05:17 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: how to get the PID of programs started? J_Szucs Programming 11 01-16-2012 02:08 AM
can't get a daemon to start - says it's already started Avatar Linux - Software 7 12-19-2006 12:02 PM
logging off by script or finding PID of session bob_man_uk Red Hat 6 10-27-2005 03:37 AM
daemon pid error! zecodela Linux - Software 0 10-23-2004 08:07 AM
Checking a daemon has started SteveGodfrey Linux - Software 1 06-01-2004 04:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration