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

如何编写每月最后一个工作日执行脚本的Cron表达式?

Hey Nick, great question—cron doesn’t have a built-in syntax to target the last workday of the month, but we can build this logic with a combination of cron scheduling and a quick check (either a shell script or a one-liner). Let’s walk through the most reliable approaches.

Solution for Running a Cron Job on the Last Workday of the Month

This approach uses a dedicated script to check if today is the last workday, then triggers your actual task only if the check passes. It’s easier to debug and maintain than a one-liner.

Step 1: Create the Check Script

Save this as last_workday_check.sh (replace paths as needed):

#!/bin/bash

# Calculate the last day of the current month (GNU date syntax, works on Linux)
last_day_of_month=$(date -d "$(date +%Y-%m-01) +1 month -1 day" +%d)
today_day=$(date +%d)
today_weekday=$(date +%u) # 1=Monday, 7=Sunday

# Logic to determine if today is the last workday
if [ "$today_day" -eq "$last_day_of_month" ]; then
    # Today is the last day of the month—check if it's a workday
    if [ "$today_weekday" -le 5 ]; then
        exit 0 # Success: run the task
    else
        exit 1 # Fail: it's a weekend
    fi
elif [ "$today_weekday" -eq 5 ]; then
    # Today is Friday—check if the last day of the month is tomorrow or the day after
    next_day=$(date -d "+1 day" +%d)
    day_after_next=$(date -d "+2 days" +%d)
    if [ "$next_day" -eq "$last_day_of_month" ] || [ "$day_after_next" -eq "$last_day_of_month" ]; then
        exit 0 # Success: this Friday is the last workday
    else
        exit 1
    fi
else
    exit 1 # Not the last workday
fi

Make the script executable:

chmod +x /path/to/last_workday_check.sh

Step 2: Configure Cron

Add this entry to your crontab (crontab -e to edit) to run your task at, say, 3 PM every day—only if the check passes:

0 15 * * * /path/to/last_workday_check.sh && /path/to/your_actual_task.sh

Method 2: One-Liner Cron Entry (For Simple Use Cases)

If you prefer not to use a separate script, you can embed the check directly into your cron line. Note: this is less readable but works for quick setups.

For GNU/Linux systems:

0 15 * * * [ "$(date +%u)" -le 5 ] && ([ "$(date -d "$(date +%Y-%m-01) +1 month -1 day" +%d)" = "$(date +%d)" ] || ([ "$(date +%u)" -eq 5 ] && [ "$(date -d "+1 day" +%d)" = "$(date -d "$(date +%Y-%m-01) +1 month -1 day" +%d)" ] && [ "$(date -d "+2 day" +%u)" -le 2 ])) && /path/to/your_actual_task.sh

Notes for Different Systems (GNU vs BSD Date)

If you’re using macOS or BSD systems, the date command syntax differs slightly. Adjust the last_day_of_month line in the script to:

last_day_of_month=$(date -v1m -v-1d +%d)

Testing Your Setup

To verify the script works, test with specific dates using the date -d flag (Linux) or date -j (macOS):

  • For a month where the last day is Saturday (e.g., June 2024):
    DATE="2024-06-28" bash /path/to/last_workday_check.sh && echo "Last workday!" || echo "Not last workday"
    
    This should return "Last workday!" (since June 28 is Friday, the last workday).
  • For a month where the last day is a weekday (e.g., July 2024, last day is Wednesday):
    DATE="2024-07-31" bash /path/to/last_workday_check.sh && echo "Last workday!" || echo "Not last workday"
    
    This should also return "Last workday!".

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:58:43