How to get PID of a script's shell? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to get PID of a script's shell?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-25-2008
alex_5161 alex_5161 is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 29 September 2014, 4:06 PM EDT
Location: Detroit
Posts: 183
Thanks: 18
Thanked 2 Times in 2 Posts
How to get PID of a script's shell?

I need to know the process ID of the shell, where a script is executing.

So, from screen I am running a script and would like to have the PID of that process. I do next:

Code:
 cat > wait.sh
a=1
while [ $a -le 120 ]; do
echo " -- $a From `basename $0` at `date +'%H:%M:%S'`"
sleep 1;
let a++;
done;
^C
> # so, script that running for 2 minuts and every secong prints out a line
>chmod 777 wait.sh
>wait.sh

From another screen I am tryin to get the PID of the wait.sh process; doing this:

Code:
ps -ef |grep "wait.sh" | grep -v "grep"

(so, looking for a line with wait.sh, excluging the current command of grepping)
- and, no doubts, I have nothing.

The is a 'sleep 1' command in the 'ps -ef' list which has parent PID of a 'bash' (I am running bash shell). But it is not only one bash. here is couple more 'bash' processes.

So, how can I get a shell PID, which actually processing a script. (I can not look for a 'sleep 1' command because actual scrip has many different commands, and it is not realistical to check for all script commands.)

Appreciate your help,
Alex
Sponsored Links
    #2  
Old 06-25-2008
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 31 October 2014, 6:22 PM EDT
Location: USA
Posts: 1,498
Thanks: 22
Thanked 146 Times in 138 Posts
$$

Each invocation of the shell stores its PID in the builtin shell variable $$. Typing echo $$ at the command line tells you the PID of the shell you are interacting with and putting it in a shell script tells you its PID.
Sponsored Links
    #3  
Old 06-25-2008
alex_5161 alex_5161 is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 29 September 2014, 4:06 PM EDT
Location: Detroit
Posts: 183
Thanks: 18
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by shamrock View Post
Each invocation of the shell stores its PID in the builtin shell variable $$. Typing echo $$ at the command line tells you the PID of the shell you are interacting with and putting it in a shell script tells you its PID.
Perfect!
Thank you, shamrock!!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
call another shell script and pass parameters to that shell script sunrexstar Shell Programming and Scripting 2 11-29-2010 02:53 PM
How to use ssh execute other shell script on other host (shell script include nohup)? orablue Shell Programming and Scripting 1 11-18-2010 08:46 PM
Accessing variables of one shell script in another shell script rsendhilmani Shell Programming and Scripting 2 03-17-2009 01:17 AM
To call/execute a shell script from a shell script konark UNIX for Dummies Questions & Answers 1 10-26-2007 05:16 PM
Have a shell script call another shell script and exit heprox Shell Programming and Scripting 2 11-20-2006 07:17 AM



All times are GMT -4. The time now is 11:16 PM.