How to get process id of called process | 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 process id of called process

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-12-2007
ysrinu ysrinu is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 25 March 2014, 1:49 PM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
How to get process id of called process

Hi,
inside my script s1.sh i am calling another script s2.sh
After s2.sh finishes, i want to print the process id of the s2.sh (that was used)
I can't use ps -ef command, since by this time s2.sh has already finished.
Is there a way to capture pid of s2.sh right when it's called?

I don't want to call s2.sh in the background, even if i did, by the time i use ps command to find the pid value, the s2.sh might have already finished in no time.

In windows script, the call to a process returns the associated pid immediately

Another question, how do i get the current pid without using the current process name?

Thanks,
-srinivas yelamanchili
Sponsored Links
    #2  
Old 11-12-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 11 April 2014, 1:02 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
I don't want to call s2.sh in the background, even if i did, by the time i use ps command to find the pid value, the s2.sh might have already finished in no time.
No need to use ps,

use echo $!

this would give the pid of the last background process and there is no constraint that the background process should be executing while echo $! value is retrieved
Sponsored Links
    #3  
Old 11-12-2007
ysrinu ysrinu is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 25 March 2014, 1:49 PM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
thanks Madhan.

Now, i really don't want to run in background.
Also i want to get the pid and return code of the called process, once it's finished.

Thanks
    #4  
Old 11-12-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 11 April 2014, 1:02 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
return code of the called process,
use echo $? - for the return value -as returned by the process
Sponsored Links
    #5  
Old 11-12-2007
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by ysrinu View Post
thanks Madhan.

Now, i really don't want to run in background.
Also i want to get the pid and return code of the called process, once it's finished.

Thanks
Not very elegant, but should work.

In your s2.sh, save the return code of the process and append the process id to it as well or vice versa. Use then accordingly in the parent script.


Code:
ret_pid=$?"#"$$
exit ${ret_pid}

Sponsored Links
    #6  
Old 11-12-2007
porter porter is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 8 January 2008, 6:50 PM EST
Posts: 2,965
Thanks: 0
Thanked 5 Times in 5 Posts
What are you going to do with the pid of a process *after* that process has finished?

As that pid is a candidate to get reused any time after the process was reaped.
Sponsored Links
    #7  
Old 11-12-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 11 April 2014, 1:02 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
Quote:
Originally Posted by porter View Post
What are you going to do with the pid of a process *after* that process has finished?

As that pid is a candidate to get reused any time after the process was reaped.
I think it should be for some logging reasons.

something like
dumper process with pid:<> completed with status: <>

for these kind of information pid info becomes mandatory to fill in the logs
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
script to monitor the process system when a process from user takes longer than 15 min run. kirankrishna3 Shell Programming and Scripting 1 09-22-2010 06:11 AM
How to run background(child) process in PHP without blocking the usual flow of parent process on Lin elizas Linux 1 02-03-2010 05:13 PM
script to monitor process running on server and posting a mail if any process is dead pradeepmacha Shell Programming and Scripting 13 03-06-2009 06:33 AM
display in a child process a command called in the parent one remid1985 Programming 7 01-19-2007 05:40 PM
Identifiy and Kill Idle Unix(SCO) Process called externally vbalajis SCO 1 12-19-2003 09:52 AM



All times are GMT -4. The time now is 06:04 AM.