Linux Cron configuration example

Source: Internet
Author: User

1. cron is a daemon that can be used to schedule the execution of repeated tasks based on the combination of time, date, month, and week.
Cron assumes that the system continues to run. If the system does not run when a task is scheduled, the task will not be executed.
Yes
To use the cron service, you must install the vixie-cron RPM package and run the crond service. To determine whether the software package is installed, use
Rpm-Q vixie-cron command. To determine whether the service is running, run the/sbin/service crond STATUS Command.

1.1. configure a Cron task
The main configuration file of cron is/etc/crontab, which includes the following lines:
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
Before
The four rows are the variables used to configure the running environment of the cron task. The value of the shell variable tells the system which Shell environment to use (in this example, It Is Bash
Shell); the PATH variable defines the path used to execute commands. The cron task output is mailed to the username defined by the mailto variable. If mailto
The variable is defined as a blank string (mailto = ""), and the email will not be sent. The Home variable can be used to set the home directory used for executing commands or scripts.
Each row in the/etc/crontab file represents a task. Its format is:
Minute hour day month dayofweek command

• Minute-minute, any integer between 0 and 59
• Hour-hour, any integer between 0 and 23
• Day-date, any integer between 1 and 31 (if a month is specified, it must be the valid date of the month)
• Month-any integer between 1 and 12 (or the month can be abbreviated as Jan or FeB)
• Dayofweek-any integer between 0 and 7 in a week. Here, 0 or 7 represents Sunday (or sun or Mon, short for a week)
• Command-the command to be executed (the command can be ls/proc>/tmp/proc or a command to execute your own script .)
In any of the preceding values, the asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value means that this command is executed every month after other constraints are met.
The short line (-) between integers specifies an integer range. For example, 1-4 means integers 1, 2, 3, and 4.
Specifies a list of values separated by commas. For example, 3, 4, 6, and 8 indicate the four specified integers.
Zheng
A slash (/) can be used to specify the interval. Adding/<integer> after the range means that integer can be skipped in the range. For example, 0-59/2
It can be used to define every two minutes in the minute field. The interval value can also be used with the star number. For example, the value of */3 can be used in the month field to indicate that a task runs every three months.
The line starting with # Is a comment and will not be processed.
For example
As you can see in the/etc/crontab file, it uses the Run-parts script to execute
/Etc/cron. Hourly,/etc/cron. daily,/etc/cron. weekly, and/etc/cron. Monthly
Scripts in the directory, which are executed hourly, daily, weekly, or monthly accordingly. The files in these directories should be shell scripts.
A cron task can be added to the/etc/cron. d directory if it needs to be executed hourly, daily, weekly, or monthly. All files in this directory use the same syntax as those in/etc/crontab.
# Record the memory usage of the system every Monday
# At 3: 30 AM in the file/tmp/meminfo
30 3 ** mon CAT/proc/meminfo>/tmp/meminfo
# Run custom script the first day of every month at 4:10 AM
10 4 1 **/root/scripts/backup. Sh
Example 1. crontab
Root
Other users can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in/var/spool/cron.
Directory, and use the user identity to create them for execution. Create a crontab project as a user, log on to the project as the user, type the crontab-e command, and use
Visual or editor environment variable specified editor to edit the crontab of the user. The format of the file is the same as that of/etc/crontab. When
After crontab changes are saved, the crontab file is saved based on the user name and written to the file/var/spool/cron/username.
.
The cron daemon checks the/etc/crontab file, the etc/cron. d/directory, and the changes in the/var/spool/cron directory every minute. If changes are found, they are loaded into the memory. In this way, you do not have to restart the daemon after a crontab file is changed.

1.2. Control the use of Cron
/Etc/cron. allow
And/etc/cron. deny files are used to restrict Cron
. The two control files are in the format of one user per line. Spaces are not allowed for both files. If the control file is modified, Cron
The daemon (crond) does not have to be restarted. The control file is read every time you add or delete a Cron task.
Regardless of the rules in the control file, the root user can always use cron.
If the cron. allow file exists, only the listed users are allowed to use Cron, And the cron. deny file is ignored.
If the cron. allow file does not exist, all users listed in cron. deny are forbidden to use cron.

1.3. Start and Stop services
To start the cron service, run the/sbin/service crond start command. To stop the service, run the/sbin/service crond Stop command. We recommend that you start the service during boot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.