简体   繁体   English

如何自动运行一长串与时间相关的CRON作业?

[英]How to automatically run a long list of time-dependent CRON jobs?

I'm hoping to run a list of CRON jobs based on TZ=America/New_York , and avoid setting CRON jobs manually (line by line), since my CRON jobs are increasing and I will also have PHP scripts that should run every minute to get data from an API. 我希望运行基于TZ=America/New_York的CRON作业列表,并避免手动(逐行)设置CRON作业,因为我的CRON作业在增加,并且我还将拥有每分钟运行一次的PHP脚本,从API获取数据。

This is my [first] pseudo bash script attempt, which should surely have several problems, and it does not seem be right to do so. 这是我的[第一次]伪bash脚本尝试,肯定会遇到一些问题,这样做似乎不正确。

  • How to make a list of time-dependent CRON jobs automated? 如何使与时间相关的CRON作业列表自动化?

  • What are the alternatives to manual CRON job setting and testing? 手动CRON作业设置和测试有哪些替代方案?

  • Maybe, would it be best/possible to write a PHP file with all my CRON jobs and then set one CRON job to run that PHP file? 也许,最好/可能用我所有的CRON作业编写一个PHP文件,然后设置一个CRON作业来运行该PHP文件?

Pseudo Bash Script 伪Bash脚本

#!/bin/bash

# There are three times that CRON jobs should be running:
# During pre-market exchanges
# During normal market hours
# During after hours or post-market exchanges

export TZ=America/New_York
server_name="$USER"

##Runs on live server
if [[ server_name=="em" ]]; then
    echo "CRON jobs only run on live servers 💛";
    exit
fi

#Market times: Before Premarket, Pre-market, Normal Market hours, Post-Market, After post market
pre_market_open=03:00
start_normal_market=09:00
end_normal_market=17:00
post_market_close=21:00

current_time=$(date '+%H:%M')
# current_time=5:23

#Read variables
IFS=: read hour_pre_market_open min_pre_market_open <<< "$pre_market_open"
IFS=: read hour_start_normal_market min_start_normal_market <<< "$start_normal_market"
IFS=: read hour_end_normal_market min_end_normal_market <<< "$end_normal_market"
IFS=: read hour_post_market_close min_post_market_close <<< "$post_market_close"
IFS=: read hour_current_time min_current_time <<< "$current_time"

#Calculate total minutes 
total_pre_market_open_min=$((10#$hour_pre_market_open*60 + 10#$min_pre_market_open))
total_start_normal_market_min=$((10#$hour_start_normal_market*60 + 10#$min_start_normal_market))
total_end_normal_market_min=$((10#$hour_end_normal_market*60 + 10#$min_end_normal_market))
total_post_market_close_min=$((10#$hour_post_market_close*60 + 10#$min_post_market_close))

total_current_time_min=$((10#$hour_current_time*60 + 10#$min_current_time))

# This function should be run during pre-market before market opens
function runCronDuringPreMarket(){
    0   0   1   1   *   sleep 19; rm -rf /home2/usco/master/blog-back/sectors >/dev/null 2>&1; wait
    3   *   *   *   *   sleep 27; rm -rf /home2/usco/master/cache >/dev/null 2>&1; wait
    6   */3 *   *   *   sleep 48; /usr/bin/php -q /home2/usco/master/cron/post/TweetUSCO.php >/dev/null 2>&1; wait
    9   *   *   *   *   sleep 12; /usr/bin/php -q /home2/usco/master/cron/equity/EQ.php >/dev/null 2>&1; wait
    echo "CRON jobs success 💚"
}

# This function should be run during normal market hours
function runCronDuringNormalMarket(){
    12  0   1   1   *   sleep 19; rm -rf /home2/usco/master/blog-back/sectors >/dev/null 2>&1; wait
    20  *   *   *   *   sleep 27; rm -rf /home2/usco/master/cache >/dev/null 2>&1; wait
    27  */3 *   *   *   sleep 48; /usr/bin/php -q /home2/usco/master/cron/post/TweetUSCO.php >/dev/null 2>&1; wait
    23,57   *   *   *   *   sleep 12; /usr/bin/php -q /home2/usco/master/cron/equity/EQ.php >/dev/null 2>&1; wait
    echo "CRON jobs success 💚"
}

# This function should be run after the market closes during post market 
function runCronDuringPostMarket(){
    29  0   1   1   *   sleep 19; rm -rf /home2/usco/master/blog-back/sectors >/dev/null 2>&1; wait
    37  *   *   *   *   sleep 27; rm -rf /home2/usco/master/cache >/dev/null 2>&1; wait
    58  */3 *   *   *   sleep 48; /usr/bin/php -q /home2/usco/master/cron/post/TweetUSCO.php >/dev/null 2>&1; wait
    3   *   *   *   *   sleep 12; /usr/bin/php -q /home2/usco/master/cron/equity/EQ.php >/dev/null 2>&1; wait
    echo "CRON jobs success 💚"
}

# This case should break based on total current time minutes 
case "$total_current_time_min" in
    before_premarket) "$total_current_time_min" -lt "$total_pre_market_open_min"; break;; 
    during_pre_market) "$total_current_time_min" -lt "total_start_normal_market_min"; runCronDuringPreMarket; wait; break;; 
    during_normal_market) "$total_current_time_min" -lt "$total_pre_market_open_min"; runCronDuringNormalMarket; wait; break;;
    during_postmarket) "$total_current_time_min" -lt "$diff_postmarket_to_end"; runCronDuringPostMarket; wait; break;;
    after_postmarket) break;;
esac

Specifications 产品规格

  • PHP7.1 PHP7.1
  • cPanel 78.0.18 cPanel 78.0.18

Cron schedule strings (29 0 1 1 *) are not understood by bash. bash无法识别Cron计划字符串(29 0 1 1 *)。 You could try to write something to parse them and decide to do something when the current time matches the pattern... but that's what Cron already does. 您可以尝试编写一些东西来解析它们,并决定在当前时间与模式匹配时做一些事情……但这就是Cron所做的。

Don't use bash for this; 不要为此使用bash; use Cron. 使用Cron。

See also crontab run every 15 minutes between certain hours 另请参阅crontab在某些小时之间每15分钟运行一次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM