how to stop the execution of .sh script

August 23, 2011 at 01:27:50
Specs: *nix
Hi!

I have the following script which ensure that a .php file will be invoked every 3 seconds.

#!/bin/bash

for (( i=0; i<43200; i++ ))
do

/usr/bin/php /var/www/vhosts/mydomain.com/httpdocs/somefile.php


sleep 3
done

I would like to be able to stop the script excecution if the time is 23:58:59

Anyone can help me?

Thanks, Zoran


See More: how to stop the execution of .sh script

Report •


#1
August 23, 2011 at 10:55:57
In classic Unix, it will be difficult to terminate a shell loop calling an external program at an exact second. One way is to use cron. Assuming the script is called myscript.ss, this cron setup kills the job at 23:59:

59 23 * * * kill $(ps -ef|grep myscript.ss$|awk ' { print $2 } ')

Keep these issues in mind:

1) this assumes you have permissions to kill the job. This is either a root cron or the user cron executing myscript.ss.

2) Keep in mind that the ps command is one of the least portable. I ran this on a Solaris 9 box. The ps command might work different on your system.

Another method is to make a change to the for loop within the script.

 #!/bin/bash

myday=$(date  +'%d')

for (( i=0; i<43200; i++ ))
do
   newday=$(date  +'%d')
   if [[ $myday -ne $newday ]]
   then
      break
   fi
   /usr/bin/php /var/www/vhosts/mydomain.com/httpdocs/somefile.php
   sleep 3
done

Sample the date and grab the date before the loop starts.  Since your stop date is close to midnight, sample the day within the loop and if the day changes, break the loop and stop.




Report •
Related Solutions


Ask Question