Linux Programmer | RHCE | RHCSA

Search This Blog

Tuesday 22 November 2022

Schedule a cron job to Run on a First/last day of every month

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

SSH not working with password after upgrade ubuntu 22.04

Issue: In recent upgrade of ubuntu 22.04 we are not able to login server with SSH password. but when we try to login with key then it allow...