How do I run a script for 1st working day of every month in cron?

Shirchabayshan

I have to set the cronjob for 1st working day of every month which means the script should run on 1st working day of every month.

If suppose 1st working is an holiday then the script should run on the 2nd working day. For eg: Jan 1 2015 is holiday then the script should run on Jan 2 2015.

John1024

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

where /path/command is replaced by whatever command you want to run on the first working day of the month. Also, replace /path/holidays with the correct path to your holidays file.

Lastly, tell cron to run the above script every day. Your command will be executed only on the first working day of the month.

How it works

Let's look at the core of the script:

  • for d in $(seq 1 $dom); do

    This starts a loop running the variable d from 1 to the current day-of-the-month.

  • today=$(date -d "$year-$month-$d" '+%x') # locale's date representation (e.g. 12/31/99)

    This sets today to the datestring for the day d of this month and year.

  • dow=$(date -d "$year-$month-$d" '+%u') # day of week: 1-7 with 1=Monday, 7=Sunday

    This sets dow to the day-of-the-week for day d with 1=Monday and 7=Sunday.

  • if [ "$dow" -le 5 ] && grep -vq "$today" /path/holidays

    The test "$dow" -le 5 ] verifies that day d is a weekday (Monday-Friday). The test grep -vq "$today" /path/holidays verifies that day d's date does not appear in the holidays file. If both tests are true, then day d is a work day and the then clause is executed:

  • then workday=Yes; nworkdays=$((nworkdays+1))

    The then clause sets workday to Yes and also increments the count of workdays so far this month: nworkdays

  • else workday=

    The else clause sets workday back to the empty string.

  • fi

    fi signals the end of the if statement.

  • done

    done signifies the end of the for loop.

  • [ "$workday" ] && [ "$nworkdays" -eq 1 ] && /path/command

    The last loop in the for statement was for today's date. Thus, if workday=Yes after the loop ends, then today is a workday. Furthermore, if nworkdays=1, then today is the first workday of the month. If both conditions are true, then your command /path/command is executed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to schedule a task to run every weekday except on the 1st day of the month

How to make it to select from 1st day every month

How do I select between 1st day of the current month and the 1st day of the following month (from current month) in postgres

Run a timer task on a specific day (1st of every month) using Spring

How do I schedule an AWS Lambda cron to run at 11:30pm on the last day of the month?

How to schedule task which will execute every 1st day of month and update a value in Firebase realtime database

How do I code a dynamic NetSuite date range that begins the 1st day of the month (4 months ago) and ends the last day of last month?

How can i run a php script on a server every month without running a cron job. e.g. using .htaccess

How do I run a script every day at 00:00 hrs to hit an django API on Ubuntu machine?

How do I make cron run something every "N"th minute, where n % 5 == 1?

Cron: Run every day except first day of the month and first day of each week

How do I run a script every time "yum update" is run?

I want to implement a spring cron scheduler that should run for the 1st time when the application is started and run every 1 hour from the next time

How do I get this script to run periodically via cron?

How can I tell cron to run a command every other day (odd/even)

Cron - How to run a job on the first weekday after a specific day of the month?

How do I compare every line of the 1st text file to every line of the 2nd text file in Python?

How do I run a python script at specific time in a day automatically?

SQL/VBA: How to group by a fiscal year starting from a day other than 1st day of month

Snowflake task to run the job every 2nd day of month (working day)

Run a cron job on the first Monday of every month?

SSRS How to get 1st date in month with a 1 day lag?

How do I get the day of the month in javascript?

How do I get the last day of a month?

How do I check day for cross month

How do I use systemd to replace cron jobs meant to run every five minutes?

How do I setup two cron jobs to run every night at 3:00 and 3:10?

Snowflake cron task bug? (Can't do for every x day of month)

How can I get a script to run every day on Mac OS X?