On a UNIX system there is always some things that should be done periodically, for example backups, locatedb updates and such things. This makes life easier, as you don’t have to keep all the things you should do in your head, or on silly little notes on your desk. Not to mention the fact that you don’t want to sit up all night doing backups.

The CRON daemon

The nice program that makes this possible is the cron daemon. It runs all the time the system is up and every minute it wakes up to see if there is something that should be done. If there is, it spawns a shell that executes some command. I’ll describe the most used cron package on Linux systems, Vixie Cron. Check out http://www.vix.com for some more interesting software.

Crontab’s

To instruct the crond when to do what, you use crontabs. Like many other programs, cron has a system and a per user crontab. They both have a similar format, with some small differences. What you do in the crontab is to give the minute, hour, day of month and/or day of week a specific command should be run.

The system crontab

The system wide crontab is most often called /etc/crontab. This file should have only important jobs, the file is only writable by root. Mine looks something like this:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don’t have to run the `crontab’
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/etc/ppp

# m h dom mon dow user command
44 5 * * * root run-parts /etc/cron.daily

07 5 * * 7 root run-parts /etc/cron.weekly

00 5 1 * * root run-parts /etc/cron.monthly

As you can see, there is seven fields with information. They are, in correct order: minute, hour, day-of-month, month, day-of-week, user and command. Some of the fields contain stars (*) which means any value will fit. The first line of my file tells cron that every day (the fields day-of-month, month and day-of-week have stars, so cron doesn’t care about them) at 05:44 in the morning I want the command “run-parts /etc/cron.daily” to be run. The command run-parts checks a directory for files that are executable by the user specified in the user field. It runs all of them, in some kind of order :) This is a good way of getting a lot of small things done without writing a big script. Makes it a lot easier maintaining the jobs. My /etc/cron.daily have the following files:

calendar* standard* sysklogd* find* man* netbase* smail* tetex-bin*

So every morning at 05:44 my computer starts doing things like rotating my logs, updating the manual index files, updating the updatedb database and other things that is quite good if you do once a day. At 05:44 I usually don’t use my computer anyway (I do actually sleep sometimes ! ;) ) so these jobs don’t use any CPU - time I could need. The other lines work just as the first, executing things every week and every month. There is a user field, if you put another user than root there, it will run the command with other rights than root. That is often recommendable by security reasons.

The per - user crontab

Every user on the system may write his own crontab. The job will be executed even though the user is not logged on. The format is similar to the system crontab, though there is not user field, since a user’s cron commands will always be executed as that user. My personal crontab looks like this:

10 5 * * 0 futility tool “+delete” “keep+1300″ ; futility pack
5 3 * * * /home/erik/bin/mirroring
15 0-23/1 * * * /usr/bin/fscan

As you can see, it has three jobs. The first one goes every Sunday (Day of week field has the value 0, which is Sunday. I could write ‘Sun’ there too) and does some packing of my fidonet message base. The second goes every day at 5 minutes past 3 in the morning and mirrors some ftp archives. The third executes 5 minutes past every hour 24 hours a day. The syntax 0-23/1 tells cron it should do it once an hour from hour 0 to hour 23. 0-14/2 would mean twice an hour from 00:00 to 14:00 in the afternoon.

The crontab command

When a user wants his own crontab, he has to install it. This is done with the command crontab. The syntax is quite simple, crontab name-of-crontab will install the file you specified as your new crontab. The command crontab has some other switches, to list, edit or remove crontabs, but the basic syntax is actually all you need to know - If you want to edit you crontab, do so and reinstall it with a new crontab command.

Environment variables and file rights.

There are a few things one should think of when trying to master cron. First cron doesn’t read your login files to set the path you’re used to, so a command that executes just fine when you test it may not run at all because cron can’t find some file. There are two solutions to this problem, the first is to use only absolute filenames (ie give the whole path to the files), the second to include something like this:

PATH = /usr/local/bin:/home/foo/bar

That way you set the environment variable PATH, so your scripts will find the files. Another environment variable of interest is MAILTO. If you set that variable (the same way as you set PATH) cron will mail the output of your commands to the address specified. Cron will by default mail the output to the user the crontab belongs to, but if you want the mail to go somewhere else you may specify it in the variable. You may also give an empty MAILTO variable (by writing “MAILTO=” in your crontab). Cron won’t mail any mails to an empty address. File rights can sometimes be a problem. If cron doesn’t want to execute your scripts or programs, try giving the group or world execution rights to the file. That may solve the problem. The irritating thing is cron won’t tell you it can’t execute, it just sits there doing nothing.

I hope that will help you in further cron adventures, peace out-

-b

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Reddit
  • Furl
  • NewsVine
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • De.lirio.us
  • Fark
  • feedmelinks
  • LinkaGoGo
  • Ma.gnolia
  • Netvouz
  • RawSugar
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

4 Comments to “Doing things periodically - Using CRON (Linux Tips)”  

  1. 1 Andre Tessmer

    Sounds like a lot of nothing. Just what I like to hear! Anyway, what I am thinking is that you could all be making some money with the amount of writing that you do. Making money online is a lot more simple than people make it out to be. Whether you are a believer or a skeptic, you should really check this out. There really is a software that can help. Even if you have never heard of press releases, article and marketing or podcasts, you soon will if you use this software. These are all cutting edge tactics that are currently used my Internet marketers and with The Free Money Formula, you get more than 120 minutes of video coaching. So if you are brand new to this, you can grab all the details in the step by step video presentations or if you are already well on your way to being a successful Internet marketer, you can skim through all of that and get right to the 2 very well-rounded pieces of software that come with this package. The 2 pieces of software that you’ll get are article marketing submission & rewriting software, and video submission software that will bring lots of traffic if used correctly. Free Money Formula is an absolutely wonderful course for newbies, and will benefit experts as well. Go ahead and check it out: http://adf.ly/1WHhr

  2. 2 Rosendo Andrle

    I most usually dont post in blogs but your Blog made me, beautiful writing. just unbelievable! The content are different but your Blog and Check Free Credit Score Blog are both the best I have read to this day. Keep up the good work!!

  3. 3 Takaful Keluarga

    Hi, i think that i saw you visited my blog so i got here to ¡§go back the prefer¡¨.I am trying to to find things to improve my site!I assume its good enough to use a few of your ideas!!

  4. 4 Consultant Marketing Montreal

    What i do not understood is actually how you’re now not actually much more well-favored than you might be right now. You are very intelligent. You understand therefore considerably in relation to this topic, produced me for my part believe it from a lot of various angles. Its like women and men don’t seem to be involved unless it’s one thing to accomplish with Lady gaga! Your personal stuffs great. At all times deal with it up!

Leave a Reply