how to capture PID for a child script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to capture PID for a child script
# 1  
Old 07-13-2009
Question how to capture PID for a child script

Hi,

I'm looking for a method where we can capture the PID and if possible the progress of child process especially the ones running in background.

can anyone help?
# 2  
Old 07-13-2009
echo the PID value to a file descriptor... e.g. >&7...
# 3  
Old 07-14-2009
how to capture PID for a child script

thanks for your response. But i found what i was looking for.
by $! we can capture the pid for the processes ran in background.
# 4  
Old 07-14-2009
Thanks aman for sharing the info...
If everybody does this the quality of this forum will surely improve.
# 5  
Old 07-14-2009
My PID $
Child PID !
echo $$
startsome &
ChildPID=$!
# wait children = done the job
wait $ChildPID

PID value is good ex. to use unique tmpfile
tmpf="/var/tmp/$$.tmp"
...
rm -f "$tmpf" 2>/dev/null
or timestamp:
Code:
stamp="$( date '+%Y%m%d%H%M%S' ).$$"

If you like to do tmp remove automatic on exit, then you must catch the exit signal

Code:
tmpf="/var/tmp/$$.tmp"

cleantmp()
{
    echo "cleaning tmp files ..."
    rm -f "$tmpf" 2>/dev/null
}

#main
trap 'cleantmp'   EXIT
do_your_job
use_tmpf "$tmpf"

# on exit your tmp file is removed, if process is not stopped using signal 9 (=and I mean it).

# 6  
Old 07-14-2009
how to capture PID for a child script

Thanks for the information about trap command. I'm stuck in a specific situation here. I have a code snippet.

Code:
#!/bin/ksh
set -vx
PID=$$
echo "parent PID =$$"
echo "calling process1"
. $TEMP_DIR/file5
stat=$?
echo $stat
echo "calling process2"
. $TEMP_DIR/file6 &
PID2=$!
export PID2
echo "PID2= $PID2"
echo "PID1= $PID"
wait "$PID2"
status=$?
echo $status
if [ "$status" = "0"  ];then
        echo "file1 successful"
else
        echo "file1 failed"
       ## exit 1;
fi

if [ $stat -eq 0 ];then
        echo "file2 successful"
else
        echo "file2 failed"
        ##exit 1;
fi

This code is capturing the exit status of both the processes ; one in forground and other in background. Now , when i change the code to

Code:
#!/bin/ksh
set -vx
PID=$$
echo "parent PID =$$"
echo "calling process1"
. $TEMP_DIR/file5 &
PID2=$!
export PID2

echo "calling process2"
. $TEMP_DIR/file6 
status=$?
echo $status
 
echo "PID2= $PID2"
echo "PID1= $PID1"
if [ "$status" = "0"  ];then
        echo "file1 successful"
else
        echo "file1 failed"
        exit 1;
fi
wait "$PID2"
stat=$?
echo $stat
if [ $stat -eq 0 ];then
        echo "file2 successful"
else
        echo "file2 failed"
        exit 1;
fi


then the process just exists after process 2 and does not return the exit codes for either of the processes. Any idea why it is happening?

Last edited by Yogesh Sawant; 07-14-2009 at 05:58 AM.. Reason: added code tags
# 7  
Old 07-14-2009
. $TEMP_DIR/file5 &
=>
$TEMP_DIR/file5 &

. $TEMP_DIR/file6
==>
$TEMP_DIR/file6


wait "$PID2"
stat=$?
= how wait worked = usually very well Smilie

If you like to test you bg job end status, I think that it's not so easy in this script. Ex. child write something status to file or ....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting child process id for a given PID

HI Am trying to get child process id for a PID using ksh.. ps -ef | grep xntpd root 3342472 2228308 0 12:17:40 - 0:00 /usr/sbin/xntpd root 4522024 6488316 0 12:18:56 pts/0 0:00 grep xntpd root 6291614 3342472 0 12:17:40 - 0:00 /usr/sbin/xntpd Here now i... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

2. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

3. Red Hat

Listing all child pid and deleting it in reverse order

Hi , My problem is that I am not able to list all process id of any process. If you see pstree command it shows many process id under https. But if I run ps command its not listing all the process id for httpd. It is just listing the PPID and immediate child process id only. I... (4 Replies)
Discussion started by: pratapsingh
4 Replies

4. Programming

How to capture messages from child process?

Hi all, I'm new in programming, but want to start writing a simple GUI for linux console application,say, wget.(for educational purpose :) ). The question is: how to start child process from C++ code and then start capture messages from its stdout? Thanks in advance. (2 Replies)
Discussion started by: vahagn_iv
2 Replies

5. Shell Programming and Scripting

PID Capture and Return Codes

I have a process that copies files from a main storage server to main other servers. We are attempting to speed up the processing and have thought that the best method would be to use concurrent file copying. What was suggested is that we change from using a simple RCP and waiting for it to... (3 Replies)
Discussion started by: dorrellg
3 Replies

6. Shell Programming and Scripting

child pid in ZSH

I am using ZSH shell in Linux. I am calling a child program in background mode parallely (say 2-3 threads). I have problem in handling the temporary files of these child programs since the temp file names are unique for all the child process. To distinguish i want to use the pid in the temp... (3 Replies)
Discussion started by: dhams
3 Replies

7. Shell Programming and Scripting

In ksh, how does an in-line child sub-process get its own PID?

This is not the same as a few of the other posted items dealing with sub-process pids (that I saw anyway). If zot contains: echo "main mypid: $$ - lastpid: $!" ( echo "block mypid: $$ - lastpid: $! - ppid: $PPID" ps -ef > xxx sleep 5 echo "block mypid: $$ - lastpid: $! - ppid:... (6 Replies)
Discussion started by: MichLab
6 Replies

8. Programming

printing ppid,child pid,pid

question: for the below program i just printed the value for pid, child pid and parent pid why does it give me 6 values? i assume ppid is 28086 but can't figure out why there are 5 values printed instead of just two! can someone comment on that! #include<stdio.h> #define DIM 8 int... (3 Replies)
Discussion started by: a25khan
3 Replies

9. Programming

Child Process PID

Hi, Can anybody solve this query? A parent process forks 2 child processes. How does the child process know it's PID without the parent process sending it. Apart from the "ps-ef" option, what other options are there if any? (2 Replies)
Discussion started by: skannan
2 Replies

10. UNIX for Dummies Questions & Answers

Script to kill all child process for a given PID

Is there any build in command in unix to kill all the child process for a given process ID ? If any one has script or command, please let me know. Thanks Sanjay (4 Replies)
Discussion started by: sanjay92
4 Replies
Login or Register to Ask a Question