shell crontabITeye - 牛牛娱乐

shell crontabITeye

2019-01-13 05:09:46 | 作者: 翠彤 | 标签: 运转,时刻,使命 | 浏览: 769

调度使命可用
crontab 可以用 vixie-cron-4.1.tar.bz2  http://download.chinaunix.net/download.php?id=22023 ResourceID=11114 来修正
java quartz web 来做
shell 版别的


#/bin/bash
# @function shell crontab 
# @author liuniuyou
# @version 0.1
# @date 2014-12-30
#CREATE TABLE `task` (
#`task_name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT  COMMENT 使命称号 ,
#`description` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 0 COMMENT 使命描绘 ,
#`bin` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 履行程序 ,
#`script` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 履行脚本 ,
#`args` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 0 COMMENT 脚本参数 多个参数加:距离 ,
#`cycle` enum(6,5,4,3,2,1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 履行周期 1周 2分 3小时 4 天 5 月 6年  ,
#`start_day` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT 0 COMMENT 数据库 默认值为0 表明 日期为空不设置 ,
#`start_time` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 开端履行时刻 由于开端 时刻 有些为天 有些 为时 不一致用字符 ,
#`times` int(11) NOT NULL COMMENT 次数 ,
#`intervall` int(10) UNSIGNED NULL DEFAULT NULL COMMENT 距离时刻 ,
#`pid` int(11) NULL DEFAULT NULL COMMENT 履行程序的pid 假如多个加:距离 ,
#`count` int(11) NULL DEFAULT 0 COMMENT 本cycle 履行周期内 现已运转的次数。运转完清零 ,
#`exec_time` int(11) NULL DEFAULT 0 ,
#PRIMARY KEY (`task_name`)
#ENGINE=MyISAM
#DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
#CHECKSUM=0
#ROW_FORMAT=DYNAMIC
#DELAY_KEY_WRITE=0
. /taskcron/common/common
exportEnv
TABLE="yt_task"
RUNBYYEAR=6
RUNBYMONTH=5
RUNBYDAY=4
RUNBYHOUR=3
RUNBYMINUT=2
RUNBYWEEK=1
MUSTRUN=1
GIVEUPRUN=0
EXISTPID=1
NOEXISTPID=0 
scriptPid=$$
#$1 cycle
#$2 starttime
getLastCycleDetail()
 starttime=$2
 cycleTime=$1
 nowTimestamp=`date +%s`
 dayTimestamp=$[$nowTimestamp-$cycleTime]
 lastCycleTime=`echo $dayTimestamp|awk {print strftime("%Y:%m:%d:%T",$0)}`
 lDayY=`echo $lastCycleTime|awk -F":" {print $1}`
 lDayM=`echo $lastCycleTime|awk -F":" {print $2}`
 lDayD=`echo $lastCycleTime|awk -F":" {print $3}`
 lDayH=`echo $lastCycleTime|awk -F":" {print $4}`
 lDayF=`echo $lastCycleTime|awk -F":" {print $5}`
 lDayS=`echo $lastCycleTime|awk -F":" {print $6}`
 case $cycle in
 "$RUNBYYEAR")
 lastStartTimestamp=`date -d "${lDayY}-$starttime" +%s`
 "$RUNBYMONTH")
 lastStartTimestamp=`date -d "${lDayY}-${lDayM}-$starttime" +%s`
 "$RUNBYDAY")
 startTime=`echo $2|awk -F" " {print $2}`
 lastStartTimestamp=`date -d "${lDayY}-${lDayM}-${lDayD} $startTime" +%s`
 "$RUNBYHOUR")
 lastStartTimestamp=`date -d "${lDayY}-${lDayM}-${lDayD} ${lDayH}:$starttime" +%s`
 "$RUNBYMINUT")
 lastStartTimestamp=`date -d "${lDayY}-${lDayM}-${lDayD} ${lDayH}:${lDayF}:$starttime" +%s`
 "$RUNBYWEEK")
 weekDay=`echo $starttime|awk -F" " {print $1}`
 startTime=`echo $starttime|awk -F" " {print $2}`
 lastStartTimestamp=`date -d "${lDayY}-${lDayM}-${lDayD} $startTime" +%s`
 esac
getNowDetail()
 now=`date +"%Y:%m:%d:%H:%M:%S"`
 nowTimestamp=`date +%s`
 nowY=`echo $now|awk -F":" {print $1}`
 nowM=`echo $now|awk -F":" {print $2}`
 nowD=`echo $now|awk -F":" {print $3}`
 nowH=`echo $now|awk -F":" {print $4}`
 nowF=`echo $now|awk -F":" {print $5}`
 nowS=`echo $now|awk -F":" {print $6}`
 nowW=`date +%w`
# $1 cycle 
# $2 Startime 
# $3 intervall 距离时刻
# $4 times 要运转的次数
# $5 count 现已运转的次数
# $6 lastExecStamp
isToExeTime()
 getNowDetail
 case $1 in
 "$RUNBYYEAR")
 startTimestamp=`date -d "${nowY}-$2" +%s`
 #isExecByYear "$2" "$3" "$4" "$5"
 "$RUNBYMONTH")
 startTimestamp=`date -d "${nowY}-${nowM}-$2" +%s`
 "$RUNBYDAY")
 startTime=`echo $2|awk -F" " {print $2}` 
 startTimestamp=`date -d "${nowY}-${nowM}-${nowD} $startTime" +%s` 
 "$RUNBYHOUR")
 startTimestamp=`date -d "${nowY}-${nowM}-${nowD} ${nowH}:$2" +%s` 
 "$RUNBYMINUT")
 startTimestamp=`date -d "${nowY}-${nowM}-${nowD} ${nowH}:${nowF}:$2" +%s` 
 "$RUNBYWEEK")
 weekDay=`echo $2|awk -F" " {print $1}`
 startTime=`echo $2|awk -F" " {print $2}`
 # 按周的不等于就抛弃 如周二运转 有必要周二运转
 if [ ${weekDay} -ne ${nowW} ];then
 return $GIVEUPRUN
 startTimestamp=`date -d "${nowY}-${nowM}-${nowD} $startTime" +%s`
 esac
 # 此处不存在 times 与 count 持平的处理,此逻辑在updateTaskCount函数中处理。 $lastExecStamp都为 前次处理的时刻戳了 所以只需加一次距离时刻就可以了。
 # ${count}为0 次运转 不需要加距离时刻。只需要判别startTimestamp 时刻
 count=$5
 lastExecStamp=$6
 runTimestamp=$[$lastExecStamp+$3]
 writeLog "count ${count} nowTimestamp ${nowTimestamp} runTimestamp ${runTimestamp} startTimestamp ${startTimestamp} " 
 if [ ${nowTimestamp} -lt ${startTimestamp} ];then
 return $GIVEUPRUN
 else
 if [ $count -eq 0 ];then
 if [ $lastExecStamp -lt $startTimestamp ];then
 return $MUSTRUN 
 else
 return $GIVEUPRUN
 if [ ${nowTimestamp} -lt ${runTimestamp} ];then
 return $GIVEUPRUN
 else
 return $MUSTRUN 
# $1 cycle 
# $2 taskName
# $3 starttime
# $4 allIntervall
updateTaskCount(){
 nowStamp=`date +%s`
 cycle=$1
 starttime=$3 
 taskIntvall=$4 
 cycleDiff=0
 case $cycle in
 "$RUNBYYEAR")
 cycleDiff=$[365*24*3600] 
 "$RUNBYMONTH")
 cycleDiff=$[31*24*3600]
 "$RUNBYDAY")
 cycleDiff=$[24*3600]
 "$RUNBYHOUR")
 cycleDiff=3600
 "$RUNBYMINUT")
 cycleDiff=60
 "$RUNBYWEEK")
 cycleDiff=$[7*24*3600]
 esac
 getLastCycleDetail "$cycleDiff" "$starttime" 
 diff=$[$nowStamp-$lastStartTimestamp]
 writeLog "updateCount:lastStartTimestamp $lastStartTimestamp starttime $3 allIntervall $4 nowStamp ${nowStamp} diff-${diff} cycleDiff ${cycleDiff} " 
 if [ ${diff} -gt ${cycleDiff} ];then
 writeLog "使命 $2 开端新的周期,更新计数器"
updateCount=`mysql -u$USER -p$PASSWORD $DATABASE EOF
UPDATE $TABLE SET count=0,exec_time=$nowStamp WHERE task_name=$2;
exit 
EOF` /dev/null 
 else
 writeLog "使命 $2 本周期内一切使命现已完结,还未到新周期的开端,等待中..... " 
#$1 数字
# 是数字 回来 1
checkInt(){ 
 if echo $1 | egrep -q ^[0-9]+$; then
 echo 1
 else
 echo 0
#$1 taskname 
#$2 CMD 运转的指令脚本
#$3 count 现已运转的次数
#$4 times 方案运转次数
#$5 Pid
execCmd()
 writeLog " 使命 $1 的 pid: $5 count $3 times $4 taskName $1 CMD $2 "
 pidIsNUll=`checkInt "$5"`
 writeLog "pidIsNUll $pidIsNUll"
 if [ $pidIsNUll -gt 0 ];then
 isRun=`exsitPid $5`
 else
 isRun=$NOEXISTPID
 writeLog "ps -a | grep "$5" | wc -l "
 writeLog "isRun :$isRun "
 if [ "$isRun" = "$EXISTPID" ];then
 writeLog "使命 $1 现已有进程在运转 "
 else
 writeLog "CMD: $2"
 CMD="$2"
 eval "$CMD" /dev/null 
 pid=$!
 execTime=`date +%s` #`date +"%Y-%m-%d:%H:%M:%S"`
 writeLog "execCmd count:$count" 
 #更行pid 和 count 
 count=$3
 if [ ${times} -gt ${count} ];then
 count=$[${count}+1]
 writeLog "execCmd2 count:$count"
 writeLog "CMD $2 UPDATE $TABLE SET count=${count},pid=$pid,exec_time=$execTime WHERE task_name=$1; " 
updateCount=`mysql -u$USER -p$PASSWORD $DATABASE EOF
UPDATE $TABLE SET count=$count,pid=$pid,exec_time=$execTime WHERE task_name=$1;
EOF` /dev/null
 writeLog "update pid exec_time: $updateCount"
 writeLog "使命 $1 指令 $2 本周期现已运转 $3, 现在开端运转第$count次 ..... "
exsitPid()
 echo `ps -a | grep "$1" | wc -l`
writeLog "taskcron 主进程pid ${selfPid}"
##初始化时刻点 2015-01-01 00:00:00 即时刻戳1420041600
`mysql -u$USER -p$PASSWORD $DATABASE EOF
UPDATE $TABLE SET exec_time=1420041600,count=0
while :
script=`basename $0`
day=`date +"%Y%m%d%H"`
LogFile="/taskcron/log/"${day}"_"${script}".log"
if [ ! -d "/taskcron/log/" ]
 mkdir /taskcron/log/
 chmod -R 777 /taskcron/log/
declare -a records
records=`mysql -u$USER -p$PASSWORD $DATABASE EOF | tail -n +2
SELECT task_name,bin,script,IFNULL(args,0),cycle,IFNULL(start_day,0),IFNULL(start_time,0),times,intervall,IFNULL(pid,0),IFNULL(count,0),IFNULL(exec_time,0),|| as split FROM $TABLE;
EOF` /dev/null
taskLines=${records}
for field in ${taskLines[@]}
 if [ "$field" = "||" ];then 
 #假如本周期的运转次数现已完结 退出循环
 if [ ${times} -eq ${count} ];then
 #当时时刻为周期时刻的结束 则更新计数器
 allInteravall=$[${times}*${intervall}] 
 updateTaskCount "$cycle" "$taskName" "$startTime" "$allInteravall"
 CMD=""
 writeLog "*****************使命 单次 $taskName end*********************"
 continue 
 writeLog "continue ...." 
 if [ "${startDay}" != "0" ];then 
 startTime=$startDay" "$startTime 
 isToExeTime "$cycle" "$startTime" "$intervall" "$times" "$count" "$lastExecTime" 
 runFlag=$? 
 if [ ${runFlag} -eq ${MUSTRUN} ];then
 execCmd "$taskName" "$CMD" "$count" "$times" "$pid"
 else
 writeLog "距离时刻为 $intervall, 本次运转时刻还没有到!等待中..... "
 i=1 
 CMD=""
 writeLog "*****************使命单次 $taskName end*********************"
 else
 case $i in
 "1")
 taskName="$field"
 writeLog "*****************使命单次 $taskName start*********************"
 "2")
 CMD="$field"
 "3")
 CMD="$CMD $field"
 "4")
 args=`echo "$field"|sed -r "s/:/ /g"` 
 CMD="$CMD $args"
 "5")
 cycle=$field
 "6")
 startDay="$field"
 "7")
 startTime="$field"
 "8")
 times="$field"
 "9")
 intervall="$field"
 "10")
 pid="$field"
 "11")
 count="$field"
 "12")
 lastExecTime="$field"
 esac
 let "i=$i+1"
 done
 sleep 1s
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表牛牛娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1

    Java中字符串的使用ITeye

    字符串,字符,比较
  • 2
  • 3

    UTLITeye

    数据,目录,文件
  • 4
  • 5
  • 6

    java与函数式编程ITeye

    函数,编程,一些
  • 7
  • 8
  • 9
  • 10