Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture child process id 1

Status
Not open for further replies.

razalas

Programmer
Apr 23, 2002
237
US
I have a script that starts a long running task in the background. I would like to capture the process id of that task when it is started. Is there a way to do that?

For example, if I have...

Code:
$ cat delay
runcobol /u/usr/acct/rsrp/DELAY -k -a $1
$ delay 5 &
[1] 786

I want to capture the "786".

I tried the following:

Code:
$ delay 5 & > pid.dat
[1] 1740
$ jobs
[1]+  Running                 delay 5 &
$ ls -l pid.dat
-rw-rw-r--  1 razalas users 0 Oct 26 11:23 pid.dat
[1]+  Done                    delay 5

I also tried nesting the background start in another script. But neither of the following would even start the background task.

Code:
$ cat temp
delay 5 & > pid.dat
$ ./temp
$ jobs
$

Code:
$ cat ./temp
delay 5 &
$ ./temp > pid.dat
$ jobs
$

Neither did the following work:

Code:
$ cat delay
runcobol /u/usr/acct/rsrp/DELAY -k -a $1 &
$ delay 5 > pid.dat
$ jobs
$

Code:
$ cat delay
runcobol /u/usr/acct/rsrp/DELAY -k -a $1 & > pid.dat
$ delay 5
$ jobs
$

Neither did any of the above options work when redirecting stderr (2>) instead of stdout.

Is this possible?

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
How about

delay 5 & DelayPid=$!

echo $DelayPid
 
M577A2,

Your suggestion is capturing the pid I need, but I don't seem to be able to do anything with it.

If echo the value out, I am unable to redirect that to a file to capture it permanently. [ponder]

Very curious! I'll keep trying.

Thanks, have a star!

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Well, in your original attempts, the line

[1] nnnn

is output by the shell, not the child process, so there's no simple way to capture it with redirection, if at all possible.

The $! method should work, and if you can output it, you can also store it somewhere:

Code:
delay 5 &
echo $! >/tmp/pid.delay

I don't see why that shouldn't work...



HTH,

p5wizard
 
How about

delay 5 & DelayPid=$!

echo $DelayPid > PidFile

Maybe I'm missing the point... [neutral]
 
P5Wizard, M5772A,

I don't know what to say...

I thought I had tried what you are both suggesting earlier (after M5772A's first reply) and it didn't seem to work, but I just tried it again, and now it's working fine!

Maybe it's because today is twice the 13th???

Anyway I am able to capture the pid, but for some reason my test "long running process" is not running. Not sure why but I'll try it with the real background process.

Thanks again.

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top