Set Cron Job for 1st working day of every month in

2019-05-11 05:01发布

问题:

I'm new to scripting language, can anyone please explain how to set the cron job for 1st working day?

回答1:

You can use the following,

@monthly    

Run once a month at the morning of the first day of the month.

0 0 1 * * /home/scripts/your_script_file.sh

3rd Edit:

This will run your job at morning say 10 AM on the first weekday of the month:

# First weekday of the month

# Monday - Friday
00 10 1-3 * * [ "$(date '+\%a')" == "Mon" ] && /home/scripts/your_script_file.sh
00 10 1 * * [ "$(date '+\%a')" == "Tue" ] && /home/scripts/your_script_file.sh
00 10 1 * * [ "$(date '+\%a')" == "Wed" ] && /home/scripts/your_script_file.sh
00 10 1 * * [ "$(date '+\%a')" == "Thu" ] && /home/scripts/your_script_file.sh
00 10 1 * * [ "$(date '+\%a')" == "Fri" ] && /home/scripts/your_script_file.sh


回答2:

First, run the date command:

$ date '+%x'
> 12/29/2014

%x tells date to display today's date in the format of the current locale. Using that exact same format, put a list of holidays in a file called holidays. For example:

$ cat holidays
> 01/01/2015
> 07/04/2015

Next, create the following shell script:

#!/bin/sh
dom=$(date '+%d') # 01-31, day of month
year=$(date '+%Y') # four-digit year
month=$(date '+%m') # two-digit month

nworkdays=0
for d in $(seq 1 $dom)
do
    today=$(date -d "$year-$month-$d" '+%x') # locale's date representation (e.g. 12/31/99)
    dow=$(date -d "$year-$month-$d" '+%u')   # day of week: 1-7 with 1=Monday, 7=Sunday
    if [ "$dow" -le 5 ]  && grep -vq "$today" /path/holidays
    then
        workday=Yes
        nworkdays=$((nworkdays+1))
    else
        workday=
    fi
done
[ "$workday" ] && [ "$nworkdays" -eq 1 ] && /path/command


回答3:

a simple one
run the scripte at 08:00 every first of the month for workdays Mon to Fri

00 8 1 * 1-5 /path/to/your_script_file.sh


标签: linux shell unix