Results 1 to 10 of 10
Hi,
Please can someone advise what I'm doing wrong with my "while do done" loop ?
My first script Setup_A.ksh runs and when it has finished and the PID = ...
- 11-09-2011 #1Just Joined!
- Join Date
- Oct 2011
- Posts
- 12
While do test issue
Hi,
Please can someone advise what I'm doing wrong with my "while do done" loop ?
My first script Setup_A.ksh runs and when it has finished and the PID = 0, I need it to start my second script BatchRun.pl. Please see my while do done loop.
See below for details :
./Setup_A.ksh
typeset -i PID
PID=`ps -ef | grep Setup_A.ksh | wc -l`
while [ $PID > 0 ]
do
echo "Setup_A PID is running = $PID"
sleep 60
PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`
done
echo "Setup_A.ksh completed"
./BatchRun.pl
thank you
- 11-09-2011 #2
I suspect that part of the problem is that PID will always return 1, and therefore it never reaches the line to execute the bash script.
If you grep the output for Setup_A.ksh, then grep will return its own process which is currently searching for Setup_A.ksh.Code:ps -ef | grep Setup_A.ksh 1002 24018 21088 0 06:22 pts/10 00:00:00 grep Setup_A.ksh
Try 'ps -ef | grep Setup_A.ksh | grep -v grep | wc -l' instead:
Code:ps -ef | grep Setup_A.ksh | grep -v grep | wc -l 0
I made a quick example. On my machine Setup_A.ksh will not be found and therefore execute the scipt I have specified:
and the output:Code:PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l` while [ $PID != 0 ] do echo "Setup_A PID is running = $PID" sleep 60 PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l` done sh new.sh
Code:krendoshazin@null:~>sh new1.sh this is new.sh
- 11-09-2011 #3Just Joined!
- Join Date
- Oct 2011
- Posts
- 12
Hi,
I have just tried this on my environment, and it does not start the second script "BatchRun.pl
" even when the PID = 0
This is my code :
#!/bin/sh
./Setup_A.ksh
typeset -i PID
PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`
while [ $PID > 0 ]
do
echo "Setup_A PID is running = $PID"
sleep 60
PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`
done
echo "Setup_A.ksh completed"
./BatchRun.pl
Is there something else I'm doing wrong within this while do done loop ?
thank you
- 11-09-2011 #4
- 11-09-2011 #5Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
Does that first script automatically go into the background somehow?
I'd do it something like this:
Code:#!/bin/sh echo "running Setup_A script:" ./Setup_A.ksh & while ps -p $! >/dev/null 2>&1; do echo script still running... sleep 1 done echo done ./Setup_B.ksh
- 11-09-2011 #6
If you are going to call a PERL script inside of shell script, you may need to point it to the PERL executable:
I cannot speak for Korn Shell, but I know Bash shells have this issue (for me at least).Code:/usr/bin/perl /path/to/perl/script
linux user # 503963
- 11-09-2011 #7Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
@scathefire,
jc, do u have that issue even when the perl script is executable and it has the correct path to the command line interpreter (e.g., #!/usr/bin/perl)?
- 11-09-2011 #8Just Joined!
- Join Date
- Oct 2011
- Posts
- 12
your version does not work for me. As the script run's in backgroud and start's multiple pids. So the first intial pid finishes and goes to the next script. But I need all the pids to complete before it starts the second script.
Also tried the orginal reply :
Yes, in my example I changed while [ $PID > 0 ] to while [ $PID != 0 ].
This did not work for me either ??
At present my script looks like ;
#!/bin/sh
./Setup_A.ksh
typeset -i PID
PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`
while [ $PID != 0 ]
do
echo "Setup_A PID is running = $PID"
sleep 60
PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`
done
echo "Setup_A.ksh completed"
./BatchRun.pl
- 11-09-2011 #9
- 11-09-2011 #10Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
Not clear to me: does Setup_A.ksh spawn sub-shells or programs, then?
Maybe something like this would work...
Code:#!/bin/bash ./Setup_A.ksh& mainpid=$! progpid=$$ get_pids(){ pids=$(ps -o pid,cmd|grep $0|grep -v $progpid) } get_pids until [ -z "$pids" ]; do echo still running.. sleep 1 get_pids ps -eo pid,ppid,cmd|grep $0|awk "\$1 !~ /$progpid/"|grep -q . || unset pids done


Reply With Quote
