If I schedule a job to be run at 3 A.M. every day and it is missed 3 days in a row, will Anacron execute it 3 times when the system comes back running, or only once ?
If it is only run once, is there a way to run it several times ?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
If you want to ensure a program is run as many times as the number of missing days whilst the system was down, you can enclose your command in a bash script as below and use a usual daily cron or anacron entry.
Start by noting in a file e.g.
date.of.lastrun the number of days since
the epoch (i.e. since jan 1st 1970) when the program was last run. You can get
this value for today with:
echo $(($(date +%s)/86400))
When cron runs this script, it reads this number from the file, calculates the number for today, and repeats your command until they match, updating the file each time.
let today=$(date +%s)/86400 lastrun=$(<date.of.lastrun) if [ ! -z "$lastrun" ] then while [ $lastrun -lt $today ] do echo "run the command for $(($today-$lastrun)) days back" # do your command ... let lastrun+=1 echo $lastrun >date.of.lastrun done else echo 'you need to: echo $(($(date +%s)/86400)) >date.of.lastrun' fi