views:

56

answers:

1

I'm running an ANT task in background and checking in 60 second intervals whether that task is complete or not. If it is not, every 60 seconds, a message should be displayed on screen - "Deploy process is still running. $slept seconds since deploy started", where $slept is 60, 120, 180 n so on.

There's a limit of 1200 seconds, after which the script will show the log via 'ant log' command and ask the user whether to continue. If the user chooses to continue, 300 seconds are added to the time limit and the process repeats.

The code that I am using for this task is -

ant deploy &

limit=1200
deploy_check()
{
while [ ${slept:-0} -le $limit ]; do
    sleep 60 && slept=`expr ${slept:-0} + 60`
    if [ $$ = "`ps -o ppid= -p $!`" ]; then
        echo "Deploy process is still running. $slept seconds since deploy started."
    else
        wait $! && echo "Application ${New_App_Name} deployed successfully" || echo "Deployment of ${New_App_Name} failed"
        break
    fi
done
}

deploy_check

if [ $$ = "`ps -o ppid= -p $!`" ]; then
   echo "Deploy process did not finish in $slept seconds. Here's the log."
   ant log
   echo "Do you want to kill the process? Press Ctrl+C to kill. Press Enter to continue."
   read log
limit=`expr ${limit} + 300`
   deploy_check
fi

Now, the problem is - this code is not working. This looks like a perfectly good code and yet, this is not working. Can anyone point out what is wrong with this code, please.

+1  A: 

If the user chooses to continue, deploy_check gets run again, but there's never another opportunity for the user to continue or cancel (although Ctrl-C could be pressed at any time). So you may want to wrap that in a while loop.

Also, pressing Ctrl-C is probably not going to kill the child process. You need to prompt for yes or no and if yes do a kill $!.

Edit:

Here is how your code flows:

  • Call deploy_check
    • Loop until slept exceeds limit (it will be 1260 at that point)
    • Break if deployed successfully
  • If it's still running, prompt the user
  • If the user presses Ctrl-C, the script exits but ant deploy is left running (unless you have a trap set elsewhere in your script that processes the Ctrl-C and does a kill on the ant deploy process)
  • If the user presses enter, the limit is raised to 1500
  • Call deploy_check testing slept==1260 against limit==1500
    • Loop until slept exceeds limit (it will be 1560 at that point)
    • Break if deployed successfully
  • The script (or this section of it) ends without prompting the user again

You would need a loop to cause the prompt to occur again.

Dennis Williamson
Also, depending on how the code in your question relates to other code that may be in the script, it may be necessary to (re)initialize `slept` to zero (or unset it).
Dennis Williamson
After the 1st time the used chooses to continue, limit is increased by 300 seconds. After 300 seconds, he'll again get the same option to continue or kill.The limit and slept variables are used only in the given piece of code and are not used anywhere else in my script. What has me bewildered is the fact that the code is working sometimes and failing to work sometimes. It's like a loose connection. :(
Bikram Agarwal
@Bikram Agarwal: The way the code is written, the user will not be prompted a second time. I was referring to how the code relates not the variables. If there is a loop around this code or deploy_check gets called elsewhere, the variable values may change in unexpected ways. Also, see my edited answer for a flow trace of your code so you can see why it never prompts the user a second time.
Dennis Williamson