5

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 ?

2

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
0

It's not possible that way, however you can convert your desired cron job to CGI script run by Apache, and then call it from external, more stable server.

Alternatively you can log each job execution to some file, then write parser for such log file, that will execute the job for missing days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.