定时任务执行 - Cron.sh

  • shell-dsr/cron.sh
#!/usr/bin/bash

# execute shell script 
###################################################
# load utils 
. ./util/utils.sh

declare -A taskDic
declare -A taskStatus

# Wednesday execute task
taskWeek="3"

# task time config
taskDic=(
["10:51"]="./updateVersionFlow.sh"
["16:45"]="./updateVersionFlow.sh"
# ["21:56"]="./task1.sh"
# ["21:57"]="./task2.sh"
)

taskStatus=()
# init task status when it is set to true, means have excuted
function initTaskStatus {
  for time in ${!taskDic[*]}
  do
    taskStatus[$time]="false"
  done
}

function printTaskList {
  echo 
  echoSuccess "----------------------------------"
  echoError "Time    |    Task"
  for key in ${!taskDic[*]}
  do
    echoSuccess "----------------------------------"
    echoWarning "[$key] | ${taskDic[$key]}"
  done
  echoSuccess "----------------------------------"
}

# exec task
function executeTask {
  while true
  do
    # set the cursor position
    setCursor 0 0

    currentWeek=$(date "+%w")
    currentTime=$(date "+%H:%M")
    displayTime=$(date "+%A %H:%M:%S")
    echo

    echoSuccess "  $displayTime  "
    printTaskList

    for time in ${!taskDic[*]}
    do
      if [ "${taskStatus[$time]}" = "false" ] &&
         [ "$currentWeek" = "$taskWeek" ] &&
         [ "$currentTime" = "$time" ]
      then
        taskStatus[$time]="true"
        . ${taskDic["$time"]}
      fi

      # if has executed
      if [ "${taskStatus[$time]}" = "true" ] &&
         [ "$currentTime" != "$time" ]
      then
        taskStatus[$time]="false"
      fi
    done
  done

}

function main {
  # clear the screen
  clear
  # init taskStatus
  initTaskStatus
  # hide cursor
  hideCursor
  # excute task list
  executeTask
}

main

posted @   箫笛  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2022-02-14 shell编程 - 条件测试
2022-02-14 shell编程 - 条件语句
点击右上角即可分享
微信分享提示