Automate processes with cron and crontab in Solaris

Contributor Icon Contributed by qmchenry Date Icon October 11, 2003  
Tag Icon Tagged: Solaris

The cron facility provides a powerful, minute-resolution process scheduler. If a process needs to run repeatedly without human intervention, an entry in the crontab file can accommodate most schedules. There are simple rules for modifying the crontab entries that must be followed.


To edit the crontab file, the crontab program must be used. The actual crontab files should not be edited directly because the contents are cached and changes will not take effect until the crond process is restarted. Using the crontab program to edit the crontabs will update the cache when the file is changed. To edit the current user’s crontab file, use:

crontab -e

The -e option tells the program to edit a copy of the user’s crontab file. The EDITOR environment variable is referenced to determine which editor to use (default is ed). To set this environment variable, see recipes for ksh and sh.

The superuser can edit a specific user’s crontab by adding the username at the end of this command. The processes run from a user’s crontab will be run as that user. Be careful with commands in root’s crontab because these will run as root and could cause problems. If shell scripts are run from root’s crontab, make sure their file permissions do not allow modification by anyone but root.

The syntax of crontab is simple. Each line represents a single scheduled task. The first five fields represent timing information and everything following is interpreted as the command to schedule. The timing fields in order are:

minutes - 0-59
hours - 0-23
days of month - 1-31
months of year - 1-12
days of week - 0-6 (Sunday-Saturday)

A variety of options work for each field. An asterisk (*) indicates all possible occurrences for that field. A number sets that single occurrence. Two numbers separated by a - indicates a range of values, and numbers separated by a comma indicate a list of occurrences.

Several examples:

15 * * * * logcheck
Runs a command called ‘logcheck’ every 15 minutes of every day.

0,15,30,45 8-17 * * 1-5 dobackup
Runs dobackup every 15 minutes (i.e., 8:00, 8:15, 8:30, and 8:45) during business hours (from 8:00 to 17:00) during business days (Monday-Friday).

Previous recipe | Next recipe |
 
  • Anonymous
    <ul id="quote">
    15 * * * * logcheck
    Runs a command called 'logcheck' every 15 minutes of every day.
    </ul>

    Above quote is incorrect. This runs a command called 'logcheck' every 15th minute of every hour of every day. I.e. 8:15, 9:15, 10:15, etc.
  • Anonymous
    So what is the correct cron entry so that it will run every 15 minutes?
  • Anonymous
    0,15,30,45 * * * * logcheck

    greetings,

    Stoyan Angelov
  • Javier
    Esta mal!

    15 * * * * logcheck --->>> esto se ejecuta a los 15 minutos de cada hora
blog comments powered by Disqus