Need to use shell script every 3 seconds to monitor the running state of a software process, found that Crond seems to only support to the points, do not know seconds, how to do?
The first method:
Of course the first thought is to write a trigger script that uses a dead loop in the trigger script to solve the problem, as follows:
Cat kick.sh
Copy Code code as follows:
#!/bin/bash
While:;d o
/home/somedir/scripts.sh 2>/dev/null &
Sleep 3
Done
Note that when you run the first time, do not use bash kick.sh & the way the background runs, it will be zombie.
You can put it on a scheduled task to run it, and then remove this entry from the scheduled task. Finally, put the script in/etc/rc.local so that it can be run every time it is powered on.
The second method:
Similar to the first method, it just feels more convenient than the first one.
Copy Code code as follows:
#!/bin/bash
#For excuting The scripts every 3 seconds in Crond.
#20100124. WXG
For ((i=1;i<=20;i++));d o
/home/somedir/scripts.sh 2>/dev/null &
Sleep 3
Done
The written crontab is then executed every minute, as follows:
Copy Code code as follows:
Crontab-e
* * * * */bin/bash/home/somedir/cron-seconds.sh * * *
The third method:
So how do you use a scheduled task to implement it directly?
The final solution is as follows, and it is validated that the script runs very stably.
Copy Code code as follows:
Crontab-e
# # for Excuting scripts.sh every 3 seconds# #on 2014-10-15
*/1 * * * */home/somedir/scripts.sh
*/1 * * * * Sleep 3 &&/home/somedir/scripts.sh
*/1 * * * * Sleep 6 &&/home/somedir/scripts.sh
*/1 * * * * Sleep 9 &&/home/somedir/scripts.sh
*/1 * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * * * * &&/home/somedir/scripts.sh
*/1 * * * * * * * * &&/home/somedir/scripts.sh
*/1 * * * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * * * &&/home/somedir/scripts.sh
*/1 * * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * sleep &&/home/somedir/scripts.sh
*/1 * * * * * sleep &&/home/somedir/scripts.sh
#-----------------------------------------------------------------
His idea is to trigger a timed task every 1 seconds, then hibernate for 3 seconds, 6 seconds, 9 seconds, and so on to execute a specific shell script.
I prefer to use a third method. Because the first method and the second method is not strict interval 3 seconds to execute, will be greater than 3 seconds, because the execution of scripts.sh also need a certain amount of time, even if the & symbol has been added to the background of the implementation will have a certain error. If the accuracy requirements are not high, the second method is recommended.
A PHP script at work is used every 3 seconds to monitor the running state of a memcached proxy server magent.
Copy Code code as follows:
Crontab-e
#-----------------------------------------------------------------
*/1 * * * */usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/dev/null 2>&1
*/1 * * * */usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/dev/null 2>&1
*/1 * * * * * Sleep 3 &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/d Ev/null 2>&1
*/1 * * * * * sleep 6 &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/d Ev/null 2>&1
*/1 * * * * * Sleep 9 &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/d Ev/null 2>&1
*/1 * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * * * &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * * * * &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * * * &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
*/1 * * * * * * sleep &&/usr/local/php/bin/php/usr/local/nginx/www/memcachedmagent/index.php 172.16.8.81 >/ Dev/null 2>&1
#-----------------------------------------------------------------