linux下shell执行秒级计划任务
【方案一】
#!/bin/bash
#执行出队
url="http://localhost/redis/push/redis_pop.php";
CURL=$(which curl)
while true
do
#$CURL $url
#$CURL $url >> /usr/local/apache2/htdocs/redis/push/push.log 2>&1
sleep 2
done
【方案二】
crontab是linux自带的计划任务程序,可以实现分,时,日,周,月。
#运行执行 sh /Application/sdns/trigger/task_crontab.sh >> /Application/sdns/log/crontab.log 2>&1
#要定时执行的脚本,注意:不使用后台运行,则若是超过10秒的话,下一次会延迟,若是使用后台执行的话,有可能出现两个任务并行的问题
dlc_cmdline="sh /Application/sdns/trigger/dotask.sh >> /Application/sdns/log/dotask.log";
#本shell脚本的执行路径
dlc_thiscmd="sh /Application/sdns/trigger/task_crontab.sh >> /Application/sdns/log/crontab.log 2>&1"
#任务执行时间间隔
dlc_task_timeout=10;
#是否后台执行deamon
is_deamon=$1;
#deamon,父进程
if [ "$is_deamon" == "--deamon" ]
then
echo "deamon start"
while [ 1 ]
do
date +"%F %T $dlc_thiscmd is started";
#调用子进程
$dlc_thiscmd
date +"%F %T $dlc_thiscmd is ended";
done
fi
#子进程的代码
while [ 1 ]
do
date +"%F %T $dlc_cmdline is started" ;
#记录本次程序开始时间
dlc_start_time=`date +%s`
#执行任务
$dlc_cmdline
#计算和时间间隔的差距
dlc_sleep_time=$(($dlc_task_timeout+$dlc_start_time-`date +%s`));
echo "sleep_time=[$dlc_sleep_time]";
#不够10秒,则sleep到10秒
if [ "$dlc_sleep_time" -gt 0 ]
then
sleep $dlc_sleep_time;
fi
date +"%F %T $dlc_cmdline is ended";
done