Specify Cron job times in SUSE 9.3

Q I am having some minor trouble with Cron jobs on my SUSE 9.3 system. Placing scripts in the cron.hourly, cron.daily and cron.weekly folders works fine, but how do I control when the files in those directories are executed? Can I set whether weekly jobs are done every Sunday or Friday and whether daily jobs are done at noon or midnight? I have tried to track down the way this works but it isn't clear. As best I can tell, there is only one Cron job scheduled in the crontabs file that runs every few minutes for all of the folder-based Cron jobs. That Cron job seems to call a script that looks in all of the cron.* directories, keeps track of successes and failures, and somehow keeps track of which jobs need to be done when.

A SUSE 9.3 does this slightly differently from some other distros. Instead of running the contents of these directories at a specific time, it runs them according to when they were last run. You've got most of the way to discovering this yourself: the single line in /etc/crontab calls the run-crons script every 15 minutes. This looks for marker files associated with each of the /etc/cron.* directories in /var/spool/cron/lastrun. If the marker file is more than an hour/day/week old, it runs the scripts in the directory and updates the timestamp on the marker. If no marker file is present, it runs the scripts and then creates one. This all means that instead of, say, running the daily scripts at 4:30 every morning when system load is low, it runs them a day after they were last run. You can force a particular time by altering the timestamp on the files in /var/spool/cron/lastrun. This script will change the timestamp of each of the monthly, weekly and daily files to 4:30 am while leaving the date unchanged (otherwise you'd never run the weekly or monthly scripts).

cd /var/spool/cron/lastrun
for i in daily weekly monthly
if [ -f cron.$i ]
touch -t $(date -r cron.$i
+%Y%m%d)0430 cron.$i

This uses the date command to extract the file's current date in YYYYMMDD format, adds the time you want (0430) and passes this to the touch command to update the file's timestamp. You can change the day for weekly scripts in a similar way. While this will switch the runtimes to a time of day more suited to you, bear in mind that any delay in running the scripts later, such as the computer being turned off at 4:30, will set the runtime to whenever the scripts were run. You could automate this by setting up a separate task in /etc/crontab to run this script at 0400 every day.

Follow us on Identi.ca or Twitter

Username:   Password: