Using cron basics

Unix Insider |   1 comment



At one time cron was easy to describe: It involved only one or two files. All you had to do was edit the files and -- voilà! -- cron did the rest. Now cron has become several files and several programs, and at first glance it seems quite complex. Fortunately, someone was clever enough to create a simplified interface along with the new complexity.



Cron is really two separate programs. The cron daemon, usually called cron or crond, is a continually running program that is typically part of the booting-up process.


To check that it's running on your system, use ps and grep to locate the process.


ps -ef|grep cron 
root    387      1   0   Jun 29 ?     00:00:00 crond 
root  32304  20607   0   00:18 pts/0  00:00:00 grep cron 


In the example above, crond is running as process 387. Process 32304 is the grep cron command used to locate crond.


If cron does not appear to be running on your system, check with your system administrator, because a system without cron is unusual.


The crond process wakes up each minute to check a set of cron table files that list tasks and the times when those tasks are to be performed. If any programs need to be run, it runs them and then goes back to sleep. You don't need to concern yourself with the mechanics of the cron daemon other than to know that it exists and that it is constantly polling the cron table files.


The cron table files vary from system to system but usually consist of the following:

  • Any files in /var/spool/cron or /var/spool/cron/crontabs. Those are individual files created by any user using the cron facility. Each file is given the name of the user. You will almost always find a root file in /var spool/cron/root. If the user account named jinx is using cron, you will also find a jinx file as /var/spool/cron/jinx.

    
    ls -l /var/spool/cron 
    -rw-------   1  root    root          3768 Jul 14  23:54  root 
    -rw-------   1  root    group          207 Jul 15  22:18  jinx 
    

  • A cron file that may be named /etc/crontab. That is the traditional name of the original cron table file.
  • Any files in the /etc/cron.d directory.


Each cron table file has different functions in the system. As a user, you will be editing or making entries into the /var/spool/cron file for your account.


Another part of cron is the table editor, crontab, which edits the file in /var/spool/cron. The crontab program knows where the files that need to be edited are, which makes things much easier on you.


The crontab utility has three options: -l, -r, and -e. The -l option lists the contents of the current table file for your current userid, the -e option lets you edit the table file, and the -r option removes a table file.


A cron table file is made up of one line per entry. An entry consists of two categories of data: when to run a command and which command to run.


A line contains six fields, unless it begins with a hash mark (#), which is treated as a comment. The six fields, which must be separated by white space (tabs or spaces), are:

  1. Minute of the hour in which to run (0-59)
  2. Hour of the day in which to run (0-23)
  3. Day of the month (0-31)
  4. Month of the year in which to run (1-12)
  5. Day of the week in which to run (0-6) (0=Sunday)
  6. The command to execute


As you can see, the "when to run" fields are the first five in the table.

1 comment

    Anonymous 3 years ago
    Can anyone tell me how I create a directory, which is named by the contents of a file?e.g I want to create a date-stamped directory for backups.I creat the file using:date > /backup/date_stampthen I want to create a sub directory using the contents of the date file just created: e.g. 16 Oct 2008so I now have a directory '/backup/16 Oct 2008' in which I can dump my backups into. If I can supress the spaces in the directory name, then even betterThanks in advance

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Ask a question

      Ask a Question