You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何配置每月第14个工作日10点执行的crontab任务?

解决每月第14个工作日10点执行的crontab任务问题

你的表达式 0 10 14 * 1-5 逻辑错误:它只会在每月14日且当天是周一至周五时执行,这和“每月第14个工作日”完全不是一回事——比如若14号是周末,任务不会触发;若当月前13天包含多个周末,第14个工作日可能早于或晚于14号。

crontab原生语法不支持直接指定“第N个工作日”,必须借助脚本实现,步骤如下:

  1. 编写判断脚本
    创建一个可执行脚本(比如/usr/local/bin/run_14th_workday_task.sh),内容如下:
#!/bin/bash

# 初始化变量
workday_count=0
current_date=$(date +%Y-%m-01)
target_date=""

# 遍历当月每一天,找到第14个工作日
while [ $workday_count -lt 14 ]; do
    # 获取当天是周几(0=周日,1=周一,6=周六)
    day_of_week=$(date -d "$current_date" +%w)
    # 判断是否为工作日(排除周日和周六)
    if [ $day_of_week -ne 0 ] && [ $day_of_week -ne 6 ]; then
        ((workday_count++))
        # 记录第14个工作日的日期
        if [ $workday_count -eq 14 ]; then
            target_date=$current_date
        fi
    fi
    # 日期+1天
    current_date=$(date -d "$current_date +1 day" +%Y-%m-%d)
done

# 对比今天是否为目标日期,是则执行任务
today=$(date +%Y-%m-%d)
if [ "$today" = "$target_date" ]; then
    # 替换成你实际要执行的命令
    echo "执行每月第14个工作日的任务"
    # /path/to/your/actual/task.sh
fi
  1. 给脚本添加执行权限
chmod +x /usr/local/bin/run_14th_workday_task.sh
  1. 配置crontab
    编辑crontab:
crontab -e

添加以下行,让脚本每天10点执行一次:

0 10 * * * /usr/local/bin/run_14th_workday_task.sh

保存退出即可。

这样,每天10点脚本会自动检查当天是否为当月第14个工作日,符合条件则触发任务。

内容的提问来源于stack exchange,提问作者pecar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 17:25:20