Most of the content below is translated according to Cron Help Guide, and some parts are added by yourself.
The full text is as follows:
Cron comes from the Greek word chronos (meaning "time"). It is the next program in linux to automatically execute specified tasks. For example, you can use cron to automatically create backups of certain files or folders during sleep every night.
Service start and stop
The cron service is a built-in service in linux, but it does not start automatically when it is started. YesRun the following command to start and stop the service:
/Sbin/service crond start
/Sbin/service crond stop
/Sbin/service crond restart
/Sbin/service crond reload
The preceding lines start, stop, restart the service, and reload the configuration.
Set cron to automatically start when it is started. Add/sbin/service crond start to the/etc/rc. d/rc. local script.
View, edit, and delete
Cron saves the command line in the crontab (cron table) file, which is usually in the/etc directory. Each system user can have their own crontab (under/var/spool/cron ). To view the current user's crontab, enter crontab-l. to edit crontab, enter crontab-e. To delete crontab, enter crontab-r. If the current identity is root, to View/Edit/delete/crontab of a user, you only need to add-u USERNAME (such as crontab-e-u USERNAME) after the corresponding command. The Default editor of the crontab file is vi. You can enter export VISUAL = 'editor' to change the default editor.
The cron service reads not only all files in the/var/spool/cron directory once per minute, but also the/etc/crontab file once. Configuring this file also allows cron to execute tasks. The crontab command is used to configure user-level tasks, and editing the/etc/crontab file is used to configure system-level tasks.
The following is an example of two cron statements (in the/etc/crontab file ). The former is used to back up the/etc directory in the evening, and the latter runs the Analog program to process server statistics.
12 3 *** root tar czf/usr/local/backups/daily/etc.tar.gz/etc>/dev/null 2> & 1
52 5 * root/usr/local/src/analog-5.32-lh/analog>/dev/null 2> & 1
The fields and fields in the cron statement are described as follows:
Month (1-12; or Jan, Feb, etc)
The day of the week (0-6, 0 is Sunday; or the abbreviation is Sun, Mon, etc)
User Name (as this user when executing the command)
Command to be executed (PATH)
Now let's look at the first line:
12 3 *** root tar czf/usr/local/backups/daily/etc.tar.gz/etc>/dev/null 2> & 1
This statement will be03:12 every day() Run the tar czf/usr/local/backups/daily/etc.tar.gz/etc command.> /Dev/null 2> & 1 indicates that all the standard output is sent to/dev/null (the recycle bin of linux), and the standard error output (2) is sent to and the standard output (1) similarly (/dev/null ). Running this command will not produce any output.
This statement can be a little more complex:
30 15 13 6 1 * root tar czf/usr/local/backups/daily/etc.tar.gz/etc>/dev/null 2> & 1
It willJune 13 on MondayRun the tar czf/usr/local/backups/daily/etc.tar.gz/etc command.
The following statement can achieve the same effect:
30 15 13 Jun Mon * root tar czf/usr/local/backups/daily/etc.tar.gz/etc>/dev/null 2> & 1
If you want to use user joey's identity15th minutes per hourTo run a program, you can use:
15 * *** joey/usr/bin/somecommand>/dev/null 2> & 1
The asterisk (*) is a wildcard, indicating that cron will ignore this field.
If you wantEvery two hoursRun a program. You can use */2 in the hour field. It will be ...... Run. The statement is as follows:
0 */2 *** joey/usr/bin/somecommand>/dev/null 2> & 1
You can also use commas (,) in cron statements to specify multiple times. For example15 and 30 minutes per hourTo run a program, you can use the minute field:
15, 30 * joey/usr/bin/somecommand>/dev/null 2> & 1
If you wantThe specified time of each day in the first week of each month (from the first day to the seventh day ).To run a program, you can use 1-7 in the date field:
15, 30 */2 1-7 ** joey/usr/bin/somecommand>/dev/null 2> & 1
This statement will beAnd 30 of every two hours on the first 7 days of each month...... 22: 15,22: 30) run the/usr/bin/somecommand command.
If you wantEvery dayExecute a script set to put all the scripts to be executed in a directory (such as/home/username/cron). You can use:
18 16 * root run-parts/home/username/cron>/dev/null 2> & 1
If you want to save the output result of a program, replace >>>/dev/null 2> & 1 with >>>/home/user/somecommand. log 2> & 1.
The following examples illustrate the problem:
0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.
Every two hours
0 */2 *** echo "Have a break now.">/tmp/test.txt
Every two hours from PM to am, am
0 23-7/2, 8 *** echo "Have a good dream :)">/tmp/test.txt
Am from Monday 4 to Wednesday every week
0 11 4*1-3 command line
Am, January 1, January 1
0 4 1 1 * command line
- View the current user's cron configuration and use crontab-l
- Edit the current user's cron configuration and use crontab-e
- Delete the current user's cron configuration and use crontab-r
- View/Edit/delete a user's cron configuration as root, and add-u USERNAME after the command
- Configure system-level tasks and edit the/etc/crontab file
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.
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, run the rpm-q vixie-cron command. To determine whether the service is running, run the/sbin/service crond status Command.
The main configuration file of cron is/etc/crontab, which includes the following lines:
MAILTO = root# If an error occurs or data is output, the data is sent to this account as an email.
HOME =/# Path of the user running. Here is the root directory.
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
The first four rows are variables used to configure the running environment of the cron task. The SHELL variable value tells the system which shell environment to use (bash shell in this example); 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 the MAILTO variable is defined as a blank string (MAILTO = ""), 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-month, any integer from 1 to 12 (or use the abbreviated month as jan, feb, and so on)
Dayofweek-any integer between 0 and 7 in a week. Here, 0 or 7 represents Sunday (or the abbreviation of a week, such as sun or mon)
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.
A forward slash (/) can be used to specify the interval. Adding/after the range means that integer can be skipped in the range. For example, 0-59/2 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.
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. scripts in the monthly Directory, which are executed hourly, daily, weekly, or monthly accordingly. The files in these directories should be shell scripts. If you want to add a task to cron, put the script for running the task (or the connection of the script) in the corresponding directory. if you remove the run-parts parameter, then you can write the complete path name of a script to be run, instead of the folder name.
The following is an example of/etc/crontab. This task runs three times a week (Monday, Wednesday, and Friday ):
02 4 ** 1, 3, 5 root run-parts/etc/cron. weekly
The following is an example of how to automatically send a diary file by email in a newsgroup:
Reply: help in crontab
Mail: Dean Thompson <Dean.Thompson@csse.monash.edu.au> date:
Newsgroups: comp. OS. linux. admin, comp. OS. linux. networking, comp. OS. linux. security
> How can I set it in the/etc/crontab file so that the daily work record file is automatically sent to the mailbox firstname.lastname@example.org? </var/log?
You can try the following example:
0 0 *** (/bin/mail email@example.com </var/log/messages)>/dev/null 2> & 1
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. As follows:
# 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
Users other than root users can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in the/var/spool/cron directory and executed using the user identity to create them. Create a crontab project as a user, log on to the project as the user, type the crontab-e command, and edit the crontab in the EDITOR specified by VISUAL or EDITOR environment variables. The format of the file is the same as that of/etc/crontab. After the changes made to crontab are saved, the crontab file is saved according to 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.
The/etc/cron. allow and/etc/cron. deny files are used to restrict the use of 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, the cron 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.
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.
Add cron jobs
When we decide to regularly execute a command on the system, we must define the command to be executed as a cron job and define the cron job to be created in the crontab settings file, set the command execution time; or you can use cron to regularly execute/etc/cron. * The SCRIPT feature of the directory. The command to be executed is created as a SCRIPT file, which is stored in this directory. Pay attention to its permissions-it must be executable!