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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tell if script is still running

Status
Not open for further replies.
Feb 12, 2002
80
NO
Hi,

I have a script that, no matter how many catches and fail-safe's I put itno it, will sometimes hang due to the data I am dealing with. I have to Ctrl+C out of it when it hangs.

I have tried many ways of catching this before it happens but it is not possible in some instances.

I am looking for a way that will allow me to set this script off at night, let it run through the night, and if it fails or hangs, will be re-set off with the next input file.

Something like:

Code:
sh my_script input_file.1

if [script is still running]
 then
     sleep 600
 else
    sh my_script input_file.2
fi
This would need a list of input files to be worked on of course.


Any ideas, pointers in the right direction, or out and out solutions appreciated!

Cheers,
lil'
 
How about

<code>
#!/usr/bin/ksh

while [ `ps -ef | grep [p]rocess1 | wc -l -ne 0 ] ; do
sleep 600
done
sh process2.sh &
exit 0
</code>




&quot;If you always do what you've always done, you will always be where you've always been.&quot;
 
Couldn't you check and amend the data to make it conform?

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Hi,

Assume you have candidate files in /some/dir and your script exits with 0 at normal termination, otherwise non zero value.

Code:
cd /some/dir
for IN_FILE in $(ls)
do
   echo "input file : $INFILE"
   date
  /path/to/your_script $IN_FILE
  if [ $? -eq 0 ]
  then
      echo "$IN_FILE : OK
   else
      echo "$IN_FILE : KO
   fi
   date
done

 
thanks for the replies - but none of them are a solution I'm afraid:

rzs0502
'ps -ef' is not possible as I may need to run two or more of these processes at once, but I am tryig not to. If that is the only way then so be it.

mrn
check and ammend the data ... I am dealing with 7TB of data, in several thousand files ... so no.

aau
I can't see how, if when my script hangs, this option will affect anything other than print out "[filename] : KO"
I need it to determine if the script has stopped or hung, not just see if it has exited.

My script works through a list of files - most files process cleanly, others fail/hang/bomb out etc. I was thinking along the lines of touching a 'flag file' every time my script itterates, and having a second script that works out the last time the flag file was touched - if it is over ... say 10 minutes, then set off the next batch of files??

Does that makes sense - something like that anyway ...


 
Could you do it with time, if it runs for over x minutes kill it?

or

Something like

Code:
#----------------------------------------------------------------
# Launch command in background and collect return code on completion
#----------------------------------------------------------------
( command < $commands > $command_log 2>&1 ; \
  echo $? > $command_return_code ) &
job_1_number=$!
tail -f $command_log | grep $search > $done_log &
job_2_number=$!
#-------------------------------
# Check done_log until complete
#-------------------------------
while [ `grep -c $search $done_log` = 0 ]
do
    sleep 1
done
#-----------------------------------------
# Tidy Up the jobs if hung and log result
#-----------------------------------------
if [ "`cat $command_return_code`" = "" ]
then
    kill -9 $job_1_number $job_2_number
    echo "command Complete at `s_date`"		   >> $process_log
else
    echo "command returned a [`cat $command_return_code`]" >> $process_log
    echo "Aborting $0 at `s_timestamp`"
    echo "========================================="
    cat $process_log
    echo "========================================="
    exit
fi

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top