Configuring the cron Service

The cron service is activated by default. It checks its configuration files every minute to see whether something needs to be done. The cron process looks for configuration at different locations:

• The generic file /etc/crontab can contain lines that tell cron when to execute a given command. This file is not edited usually on SUSE Linux Enterprise Server.

• In the directory /etc/cron.d, an administrator can put a file that defines what should happen and when it should happen.

• Every user can have their own cron configuration file, telling the system when to execute certain tasks.

• The directories /etc/cron.hourly, cron.daily, cron.weekly, and cron.monthly activate jobs once an hour, a day, a week, or a month, respectively. These directories contain files that are activated every hour, day, week, or month. The crontab process evaluates these directories every 15 minutes, so it can take up to 15 minutes before a process is first started this way.

Working with the cron.hourly (and so on) mechanism makes it easy for an administrator to start jobs on a regular basis. The scripts in these directories are just regular shell scripts that make sure the job is done. This means all you have to do is include a shell script that activates the job you want to start. These scripts can be simple; just a line that starts the service is enough. You should be aware that the scripts in /etc/cron.hourly, cron.daily, cron.weekly, and cron.monthly are default scripts that are placed there when you install your server. When you perform an update, these directories will be overwritten. Therefore, make sure any modifications you make to these directories are put in /etc/cron.hourly.local (and so on); this ensures that your modifications will still exist after an update.

Note You need to restart some daemons to make sure the changes are activated. This is not true for cron, which will reread its configuration every minute to check whether any new jobs have been scheduled.

Configuring Cron User Jobs

You can set up your system to allow individual users to start their cron jobs. Such a configuration starts with the files /var/spool/cron/allow and /var/spool/cron/deny. A user who is listed in /var/spool/cron/allow or who is not listed in /var/pool/cron/deny is capable of adding cron jobs. If /var/spool/cron/allow exists, only this file is evaluated, and the settings in /var/spool/cron/ deny are ignored. If both files don't exist, only root can create cron jobs. The cron configuration files for individual users are stored in the directory /var/spool/cron/tabs. You can use the crontab command to edit this file. Next you can see some examples of the use of the crontab command:

crontab -e: This creates or edits cron jobs for the user who executes the command. You can use the editor vi to modify these files.

crontab -l: This command lists all the jobs that are scheduled for the current user. crontab -r: This command deletes all the jobs for the current user.

In the cron files, you use lines to define what should happen. On each line, you specify one command. Each line consists of six fields. The first five fields specify when the command should be activated, and the last field specifies what command should be activated. Next you can see an example of such a line:

The easiest part to understand in the previous line is the command. This is the command fetchmail mailserver. This command makes sure incoming mail is fetched from the mail server. Then in the first five fields, you can see an indication of the times that should happen. These fields have the following meaning:

Minutes: This field specifies the minute when the command should be executed. This field has a range from 0 to 59. Always specify something for this field, because if you don't, the command will run every minute. In the example, the construction */5 specifies that the command should run every five minutes; specifically, * refers to every minute, and the construction /5 is used as a modifier that specifies it should run every five minutes only.

Hours: This field specifies the hour the command should run. Possible values are from 0 to 23. In the previous example, you can see that the command will run every hour from 8 to 18.

Day of the Month: Use this field to execute a command only on given days of the month. Often this field is not specified.

Month: Use this field to specify in which month of the year the command should run.

Day of Week: This field specifies on which day of the week the command should run. The range is from 0 to 7, and the values from 0 and 7 both specify that the command should run on Sunday.

Note You usually will not use it, but if you ever want to work with the /etc/crontab file, be aware that between the time setting and the command you want to execute, you enter the name of the user whose account should be used to execute the command. For example, 0 17 * * * root /sbin/shutdown -h now will make sure the system shuts down automatically every day at 5 p.m. by using the permissions of the user root.

Continue reading here: Executing Once with at

Was this article helpful?

0 0

Responses

  • PORTIA
    How to start cron sevice in suse linux?
    9 years ago
  • TUOMAS JOKELA
    How to restart cron suse?
    9 years ago
  • Paul
    How to start crontab service in susue?
    9 years ago