Schedule cronjob every second time it runs

August 29th, 2011 | Tags:

I needed a way to run a cronjob every other Saturday. You could alter this for other scenarios as long as you set the correct schedule in your crontab.

0 3 * * 6 /root/scripts/MYSCRIPT.sh

The first time cron runs, it should detect that no file is present (in this case “/tmp/cron_control”), which in turn creates the file and exits. The next time cron is scheduled, it will see that the file exists, removes it and proceeds to run the rest of the code. If you want your script to run at the very next scheduled timeslot, you could reverse this and create the “/tmp/cron_control” beforehand.

#!/bin/bash

CRON_CONTROL="/tmp/cron_control"

if [ -f ${CRON_CONTROL} ]
then
     rm -f ${CRON_CONTROL}
else
     touch ${CRON_CONTROL}
     exit 0
fi

... RUN THE REST OF CODE ...

exit 0
No comments yet.