|
Home > Archive > Unix Shell > December 2006 > to find the PID of a background ksh function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
to find the PID of a background ksh function
|
|
| mobidyc 2006-12-08, 1:23 pm |
| Hello,
i can't obtain the PID of a background ksh function, could you help me
to find the correct way please? :
##########PROGRAM##############
#!/bin/ksh
SERVERS="nr0u0151 nr0y0031 nr0u0160"
TMPDIR=/tmp
DEBUG=true
remote_command () {
SERVER_bg="${1}"
shift
COMMAND_bg="${@}"
ssh -f -lroot $SERVER_bg \"${COMMAND_bg}\" >> $TMPDIR/$SERVER_bg 2>&1
if [ "$?" != "0" ]
then
echo "ssherr" >> $TMPDIR/$SERVER_bg
remsh -l root $SERVER_bg "${COMMAND_bg}" >> $TMPDIR/$SERVER_bg
2>&1
if [ "$?" != "0" ]
then
echo "rsherr" >> $TMPDIR/$SERVER_bg
fi
fi
}
for SERVER in $SERVERS
do
touch $TMPDIR/$SERVER
chmod 600 $TMPDIR/$SERVER
remote_command $SERVER "uname -n ; uname -s" & # Backgrounded
process here
PID="$!"
eval ${SERVER}_PID=\$PID
[ -n "DEBUG" ] && {
eval echo eval: ${SERVER}_PID=\$PID
echo "PS pid: $(ps -ef -o pid,args |awk '$1 ~ /^'"$PID"'$/')"
echo "PS cmd: $(ps -ef -o pid,args |grep "ssh -f -l root $SERVER"
|grep -v grep)"
}
done
##########END PROGRAM##########
result:
# ./t
eval: nr0u0151_PID=15695
PS pid: 15695 /bin/ksh ./t
PS cmd: 15697 ssh -f -lroot nr0u0151 "uname -n ; uname -s"
eval: nr0y0031_PID=15737
PS pid: 15737 /bin/ksh ./t
PS cmd: 15738 ssh -f -lroot nr0y0031 "uname -n ; uname -s"
eval: nr0u0160_PID=15768
PS pid: 15768 /bin/ksh ./t
PS cmd: 15771 ssh -f -lroot nr0u0160 "uname -n ; uname -s"
as you can see, the "$!" value is not the correct value, it's the value
of the mail program.
please help me to understand!!!
--
Regards,
Mobidyc
| |
| mobidyc 2006-12-08, 1:23 pm |
| "mobidyc" <mobidyc@msn.com> a écrit dans le message de
news:elc46t$5mv$1@talisker.lacave.net...
> Hello,
>
> i can't obtain the PID of a background ksh function, could you help me
> to find the correct way please? :
>
> ##########PROGRAM##############
> #!/bin/ksh
> SERVERS="nr0u0151 nr0y0031 nr0u0160"
> TMPDIR=/tmp
> DEBUG=true
>
> remote_command () {
> SERVER_bg="${1}"
> shift
> COMMAND_bg="${@}"
>
> ssh -f -lroot $SERVER_bg \"${COMMAND_bg}\" >> $TMPDIR/$SERVER_bg
2>&1
> if [ "$?" != "0" ]
> then
> echo "ssherr" >> $TMPDIR/$SERVER_bg
> remsh -l root $SERVER_bg "${COMMAND_bg}" >> $TMPDIR/$SERVER_bg
> 2>&1
> if [ "$?" != "0" ]
> then
> echo "rsherr" >> $TMPDIR/$SERVER_bg
> fi
> fi
> }
>
> for SERVER in $SERVERS
> do
> touch $TMPDIR/$SERVER
> chmod 600 $TMPDIR/$SERVER
>
> remote_command $SERVER "uname -n ; uname -s" & # Backgrounded
> process here
> PID="$!"
> eval ${SERVER}_PID=\$PID
> [ -n "DEBUG" ] && {
> eval echo eval: ${SERVER}_PID=\$PID
> echo "PS pid: $(ps -ef -o pid,args |awk '$1 ~ /^'"$PID"'$/')"
> echo "PS cmd: $(ps -ef -o pid,args |grep "ssh -f -l root
$SERVER"
> |grep -v grep)"
> }
> done
> ##########END PROGRAM##########
>
> result:
> # ./t
> eval: nr0u0151_PID=15695
> PS pid: 15695 /bin/ksh ./t
> PS cmd: 15697 ssh -f -lroot nr0u0151 "uname -n ; uname -s"
> eval: nr0y0031_PID=15737
> PS pid: 15737 /bin/ksh ./t
> PS cmd: 15738 ssh -f -lroot nr0y0031 "uname -n ; uname -s"
> eval: nr0u0160_PID=15768
> PS pid: 15768 /bin/ksh ./t
> PS cmd: 15771 ssh -f -lroot nr0u0160 "uname -n ; uname -s"
>
> as you can see, the "$!" value is not the correct value, it's the
value
> of the mail program.
of course, i would liked to write 'of the main program' ;)
> please help me to understand!!!
>
> --
> Regards,
> Mobidyc
i have resolved my problem by creating a program instead of a function
bye
| |
| Michael Paoli 2006-12-09, 7:35 am |
| mobidyc wrote:
> i can't obtain the PID of a background ksh function, could you help me
> to find the correct way please? :
First of all, you could ask your question much better - most notably
by being much more concise in providing only code portions relevant to
the question/problem at hand. See also:
http://catb.org/esr/faqs/smart-questions.html#volume
<code snipped down from approximately 53 lines of code and output>
You've also got some temporary file handling security issues
with the example code you provided (and possibly other issues), but
that's another subject.
> remote_command () {
> }
> remote_command $SERVER "uname -n ; uname -s" &
> PID="$!"
> eval ${SERVER}_PID=\$PID
> eval echo eval: ${SERVER}_PID=\$PID
> echo "PS pid: $(ps -ef -o pid,args |awk '$1 ~ /^'"$PID"'$/')"
> echo "PS cmd: $(ps -ef -o pid,args |grep "ssh -f -l root $SERVER"
> |grep -v grep)"
>
> eval: nr0u0151_PID=15695
> PS pid: 15695 /bin/ksh ./t
> PS cmd: 15697 ssh -f -lroot nr0u0151 "uname -n ; uname -s"
>
> as you can see, the "$!" value is not the correct value, it's the value
> of the mail program.
mail? Perhaps you meant main in your question?
$$ is the PID of the shell process itself, $! of the last
background/asynchronous process forked.
Why all the complication with awk and grep in looking for the PID(s)?
ps(1) will take PID(s) as argument(s) (precise syntax may vary
depending upon your flavor of ps(1)).
The command you're backgrounding is a shell function, so it's not
surprising to find a separate (forked) instance of the same shell
as that background process.
These concise examples might help you understand:
$ sh -c 'f(){ sleep 2;};f & ps p $$,$!'
PID TTY STAT TIME COMMAND
22465 pts/0 S+ 0:00 sh -c f(){ sleep 2;};f & ps p $$,$!
22466 pts/0 S+ 0:00 sh -c f(){ sleep 2;};f & ps p $$,$!
$ sh -c 'f(){ exec sleep 2;};f & ps p $$,$!'
PID TTY STAT TIME COMMAND
22471 pts/0 S+ 0:00 sh -c f(){ exec sleep 2;};f & ps p $$,$!
22472 pts/0 S+ 0:00 sleep 2
|
|
|
|
|