How to configure cron job to run every 2 days at 11PM

Solution 1:

You can use the following cron arrangement. The fields denote (from left-to-right):
Minute, Hour, Day of Month, Month, Day of Week. The "*/2" in the Day of Month field means "every two days".

0 23 */2 * * insert_your_script_here.sh

Solution 2:

In general, you need to use crontab to define the task and the run schedule.

e.g.

crontab -e -u root

This will put you in VI editing root's crontab entry. Then as ewwhite says, enter:

0 23 */2 * * insert_your_script_here.sh

and then [^ESC] ZZ to save the changes.

This is a good first attempt, but this is not quite every alternate day, as it will run on the 30th of the month and then next run on the 2nd of the month. If you really do need it to be every 2nd day, then run the script EVERY night.

0 23 * * * insert_your_script_here.sh

and in the start of the script use

#!/bin/sh
if [ -f /tmp/altday.txt ]; then
  rm /tmp/altday.txt
  exit
fi
touch /tmp/altday.txt

This uses a text file to force the script to exit every alternate invocation.

Tags:

Linux

Cron

Centos