How to write shell scripts that execute batch jobs with crontab only at month-end

Tadashi Shigeoka ·  Thu, December 7, 2017

I’ll introduce how to write shell scripts that execute batch jobs with crontab only at month-end.

Linux | リナックス

Shell script that executes specific processing at month-end

if /usr/bin/test $( date -d '+1 day' +\\%d ) -eq 1
then
  echo "1st day of the month"
fi

How to write crontab that executes only at month-end

Here’s sample crontab code that executes the shell script /path/to/script.sh at 3 AM on the last day of the month:

0 3 28-31 * * /usr/bin/test $(date -d '+1 day' +\\%d) -eq 1 && /path/to/script.sh >> /path/to/your.log 2>&1

Since month-end dates are only between 28-31, cron executes on these dates.

That’s all from the Gemba that wants to execute processing at month-end with cron.

Reference Information