Set up a Cron job 1 working day of every month in Shell Scripting

I am new to scripting, can anyone explain how to set up a 1 business day cron job?

+3


source to share


4 answers


First run the date command:

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

      

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



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

      

Then 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 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

      

0


source


You can use the following,

@monthly    

      

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

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

      



3rd Edit:

This will start your job in the 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

      

+4


source


Stumbled upon this old question and I did it by simply giving:

0 59 6 ? * MON#1

      

Starts at 6:59 am of every month on the first Monday.

0


source


simple
script run at 08:00 every first month for workdays Mon to Fri

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

      

-1


source







All Articles