[HOWTO]Set crontab timing

Had your computer crash on you, or a website shows wrong, or your printer went dead on you? Come on in...

Moderator: Crew

User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS

[HOWTO]Set crontab timing

Post by Maz »

Problem: How to set exact times when cron executes scripts?
Usefull especially when transferring data. It's better to transfer data at night, when
others do not need network ;)

One way to solve the problem:
The times when cron launches commands are set in file /etc/crontab

Here's an example of crontab file:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run­parts
01 * * * * root run­parts /etc/cron.hourly
02 4 * * * root run­parts /etc/cron.daily
22 4 * * 0 root run­parts /etc/cron.weekly
42 4 1 * * root run­parts /etc/cron.monthly
You can make comments in the file, by adding # mark at the beginning of the
comment line.
Now, the runtimes are set in lines below the run­parts comment
far left is the folder from which the scripts are being executed at the time the line
specifies. Usually as names of those folders do suggest, scripts in cron.hourly folder
are executed once/hour, cron.daily once / day etc.

I think run­parts word tells that it is folder, and that all scripts in it should be
executed.
root is most probably the user whose privileges will be used when executing the
scripts.
then there's places for 5 values. (values or asterix)
first is the minute in wich the script will be executed. As you see, cron.hourly has only
the minute specified (* means that all values are ok). It is set to 01, so all scripts in
cron.hourly will be executed at the first minute of an hour. And since there's no other
values specified, it means that regardless of what hour/day/week etc. it is, scripts will
be executed if minute is 01 = every hour.
Next is hour. As you see, cron hourly scripts are to be executed every time hour is 04
and minute 02. That means 04:02 AM every day.
Third is day of month, fourth is month and last one is day of week. Beware day of
week. Sometimes cron is set to start the week from sunday, sometimes from monday.
Well, luckily at least cron with fedora is set to accept not only numeric values, but
also 3 first letters in the name of day. IE. Mon instead of 1...
User avatar
Zyx
Pretender to the throne
Posts: 1909
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki

Post by Zyx »

of course, if you don't run your linux/bsd/mac box 24/7, you might want to install anacron instead...
Do you has what it takes to join the Homestarmy? The guts? The determination? The five bucks? Join today!
User avatar
Maz
Admin emeritus
Posts: 1938
Joined: Thu Mar 16, 2006 21:11
Location: In the deepest ShadowS

Post by Maz »

Indeed Zyx :)
Oh, btw... I am quite positive some users could perhaps appreciate a little guide for anacron ;) Feel free to help us to help others :)
User avatar
Zyx
Pretender to the throne
Posts: 1909
Joined: Wed Mar 29, 2006 20:48
Location: Helsinki

Post by Zyx »

hmm... this might be a bit late, seems I totally forgot about this and og. anway...

anacron is a fun tool to have if you don't run your *nix-type computer 24/7, as unlike cron, it does the (routine maintenance) tasks when certain amount of time is passed (whereas cron executes tasks at a specific time). So, when cron runs something every week on thursdays at 2pm, anacron will run stuff every 168 hours. Otherwise it works pretty much like cron. Available in your favourite *nix-like operating system's distribution's repositories! Might even be installed by default in latest Ubuntu, if my memory serves right.

"man anacron" says more about the program than I'll ever can. I also updated to OS X Tiger, so no need for anacron anymore - long live launchd.