Operating System - HP-UX
1756545 Members
2725 Online
108848 Solutions
New Discussion юеВ

Re: PID with pipe command

 
Siva_13
Occasional Advisor

PID with pipe command

Hi,

We are running a command and piping the output to 'tee'

cmd | tee -a sample.log &
print "$!" > pid_file

Since we are using the pipe, the PPID is getting stored in the pid_file.

Could anyone tell us how to get the PID in the pid_file.

The PID stored in the pid_file will be later used for killing the 'cmd' using a
'stop script'.

Could anyone tell how we can get this functionality?

Thanks,
Siva
4 REPLIES 4
Mark Greene_1
Honored Contributor

Re: PID with pipe command

Unless the "cmd" is really a shell script, you cannot, it's a timing issue. Your best bet is to change the print $$ to a ps -ef |grep cmd|grep -v grep|tr -s " " |cut -d" " -f3>pdi_file to extract the PID from the ps listing.

mark
the future will be a lot like now, only later
Hein van den Heuvel
Honored Contributor

Re: PID with pipe command


$ perl -e 'sleep 1000; print "done\n"' | tee -a x.x &
[1] 15219
$ print $!
15219
$ ps -f
UID PID PPID C STIME TTY TIME COMMAND
root 15219 4146 0 07:29:04 pts/0 0:00 tee -a x.x
root 15220 15219 0 07:29:04 pts/0 0:00 perl -e sleep 1000; print "done\n"
root 15221 4146 1 07:29:17 pts/0 0:00 ps -f
root 4146 4138 0 Nov 2 pts/0 0:00 -sh

$ ps -f | awk -v pid=$! '{ if ($3==pid){print $2}}'
15220

hth,
Hein.

Fred Ruffet
Honored Contributor

Re: PID with pipe command

You can do it using perl :

perl -e 'if (!($pid=fork())) { exec("sleep 30") } print "$pid\n";' | read childPID

this will return pid of subprocess in childPID env var. Note that this is really basic code, and you may need to enhance fork management, redirect STDIN and STDOUT for complete separation...

Regards,

Fred

--

"Reality is just a point of view." (P. K. D.)
Fred Ruffet
Honored Contributor

Re: PID with pipe command

I forgot the proof / sample (I tested and it worked :)

#perl -e 'if (!($pid=fork())) { exec("sleep 30") } print "$pid\n";' | read childPID
#echo $childPID
27507
#ps
PID TTY TIME COMMAND
27094 pts/tb 0:00 sh
27507 pts/tb 0:00 sleep
27093 pts/tb 0:00 telnetd
27508 pts/tb 0:00 ps

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)