0

I'm posting a portion of the sh script that we use with cron to take a cold backup of our Oracle EBIZ. It works most of the times, fails once in a while because one of the processes the script monitor, fails to stop/terminate

Code portion

    su - applprod -c /home/applprod/scripts/Apps_stop.sh
    FNDPROCESS=`ps -ef | grep FNDLIBR | grep applprod | wc -l`
    echo 'We have still running ' $FNDPROCESS ' FNDLIBR for PROD'
    echo 'Please wait...for ' $FNDPROCESS ' seconds!!'

    NPROCESS=$FNDPROCESS
    while [ $FNDPROCESS -gt 0 ]
    do
    FNDPROCESS=`ps -ef | grep FNDLIBR | grep applprod | wc -l`
    if [ $FNDPROCESS -ne $NPROCESS ]
      then
    echo 'We have still running ' $FNDPROCESS ' FNDLIBR for PROD'
    echo 'Please wait...for ' $FNDPROCESS ' seconds!!'
    sleep $FNDPROCESS
    NPROCESS=$FNDPROCESS
    fi
    done    

Usually the loop finishes within 3-4 minutes, unless one of the FNDLIBR processes fails to

Now we need to add a max 30 minutes wait time for the loop above, thus, if the wait time is exceeding 29 Minutes, the script should forcefully kill all the processes matching 'FNDLIBR'(Concurrent manager) and come out of the loop.

Please help me :)

Thanks and regards,

Rajesh Thampi
  • 439
  • 7
  • 15
  • Maybe this questions has already been answered: http://stackoverflow.com/a/11198713/5396827 – LaPriWa Dec 26 '15 at 06:17
  • Hello @LaPriWa thank you. I am kind of too new to shell scripts. I've checked the post you suggested and it looks like I can change my existing script(s) to reflect the seconds based loop. Thank you :) – Rajesh Thampi Dec 26 '15 at 07:11

2 Answers2

1

You can use sleep 1740 Your program will pause for 29 minutes and below this line you can write the code which kills all processes which are still running.

Akshay
  • 45
  • 5
  • First read your answer sounds quite promising :), however, as I said with my query, the loop gets stuck once in a while. So, waiting 29 minutes during each run is kind of an overkill when the entire loop gets completed in 3-4 minutes in usual cases. Yet, I can sure consider your suggestion. Thank you – Rajesh Thampi Dec 26 '15 at 07:04
  • Don't sleep within loop. Sleep before it for 29 minutes and then start the loop with no sleep in it and then the loop will kill all the processes in it. – Akshay Dec 26 '15 at 07:35
0

I would use the following approach:

  • start your shell script using timeout and specify both max elapsed (29 minutes?) and signal to send if/when the timeout is exceeded (I'd suggest SIGUSR1, SIGUSR2 or so)
  • modify you shell script in order to "trap" the signal here above and kill the processes matching FNDLIBR

This way the process will take 3 or 4 minutes when everything's ok or max 30 minutes when the script monitor fails to terminate.

mauro
  • 5,730
  • 2
  • 26
  • 25