Execute cron job for first day of every month.
10 10 1 * * /tmp/test.sh
The above creates a cron job that executes every 10:10 AM each month on the first day.
Execute cron job on last day of every month.
Determining the last day of the month can be challenging as some months have 28, 29, 30, or 31 as the last date. Besides, the field for the Day_of_Month takes a specific day and giving it a list of possible days would execute on an incorrect day.
The trick is to specify a command that checks if the next day is the first day of the month. If so, it means the current day is the last of that month and the cron job can then execute.
The command below checks and returns tomorrow’s date:
date +%d -d tomorrow
we can set it to run our cron job.
The new command would be:
30 13 28-31 * * [ "$(date +\%d -d tomorrow)" = "01" ] && /tmp/test.sh