1832610 Members
2439 Online
110043 Solutions
New Discussion

Re: korn shell issues

 
SOLVED
Go to solution
Fred Myers_1
Occasional Contributor

korn shell issues

I asked earlier how to get a process id from a script that is run, automaticaly via Maestro.

I have found that Korn shell sets a variable $! equal to the PID of the last background process run, I am doing a process that comes back after it is entered and runs for several hours, but it will not set the PID with $!, can anybody help?

Thanks
8 REPLIES 8
Michael Steele_2
Honored Contributor

Re: korn shell issues

What about $$? Returns the last PID.

# echo $$
Support Fatherhood - Stop Family Law
curt larson_1
Honored Contributor

Re: korn shell issues

can you provide a small sample of what your trying to do

try doing this

#!/usr/bin/ksh

print "1this shells pid is $$"
( print "this shells pid is $$;sleep 5) &
print "the background process pid is $!"
sleep 6
print "the background process pid is $!"
print "2this shells pid is $$"

maybe this will help
Steven E. Protter
Exalted Contributor

Re: korn shell issues

A common technique I use for storing data unique to the PID is.

tfile=/tmp/process$$.dat

# process stores data in the file Does work.

rm $tfile
exit 0;

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Graham Cameron_1
Honored Contributor
Solution

Re: korn shell issues

Works for me:

# sleep 60 &
[1] 255
# echo $!
255

Maybe there's some confusion. The $! variable is set when you have run a background command with "command &".
When you say your process "comes back after it is entered", maybe it is a daemon process, which behaves differently, by spawning a child process and terminating the original. This will not set $!.

I think you were running the Progress DB, and if this is anything like Oracle then its processes do run as daemons.

From another window, if you can find the pid of your "child" process, and run "ps -fpPID", then look in the PPID (parent) column. If PPID matches your shell then it is a true child; if PPID = 1 then it is a daemon.

I can explain further but it gets a bit technical. In a nutshell, if you run a child process with "command &", you will see $!. If you don't, you won't. In the former case you can also use the "wait" command to wait for all children to finish, in the latter you can't, because the daemon is not a true child.

Hope this helps.

--Grahan
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.

Re: korn shell issues


You can get the current PID with $$. I always use this variable to define uniqueness files.

Ex:

#! /bin/ksh
#
#
ls -la > file.$$
rm file.$$

where:

$$ -> PID of the Korn Shell that is interpreting the script.

Tania
If you want something, you need to persist to get that.
Fred Myers_1
Occasional Contributor

Re: korn shell issues

Grahm, thanks! yes I do belive it is running a demon process. Is thier an easy way you know of to trap when this demon process completes?

Thanks
Graham Cameron_1
Honored Contributor

Re: korn shell issues

I don't know of an easy way.
The "wait" command won't help here, this doesn't work for daemons.
I think the traditional way is to loop, running ps -fpPID, until the process exits, as I think you are already doing.
If anyone knows a better way I'd be interested to hear it.
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
john korterman
Honored Contributor

Re: korn shell issues

Hi Fred and Graham,
based on something I once saw mr. A. Clay Stephenson do....

First, make a script that simulates something running for a period of time, e.g. ./runback.sh

#!/usr/bin/sh
sleep 20

Then use the following as the controlling script:

#!/usr/bin/sh
BACK_JOB=./runback.sh
BACK_PID=""
RC=0
# start background job
$BACK_JOB &
BACK_PID=${!}
while [[ $RC = 0 ]]
do
kill -s 0 ${BACK_PID} 2>/dev/null
RC=$?
sleep 5
echo $BACK_PID is running
done
echo $BACK_PID has stopped

The controlling script tests for the existence of the background pid by issuing the kill -s 0 command; the moment it does not return zero it is considered stopped.
Perhaps an idea?

regards,
John K.
it would be nice if you always got a second chance