Cron comes from the Greek word chronos (meaning "time"), which is the next program in the Linux system that automatically executes the specified task. For example, if you want to create a backup of some files or folders every night while you sleep, you can use cron to come from execution.
Service start-up and stop
The cron service is a built-in service for Linux, but it does not boot automatically. You can start and stop the service with the following command:
/sbin/service crond Start
/sbin/service Crond Stop
/sbin/service crond Restart
/sbin/service Crond Reload
The above 1-4 lines start, stop, restart the service, and reload the configuration, respectively.
To set cron to start automatically when booting, add/sbin/service crond start in 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 you are currently root, to view/edit/delete/a user's crontab, simply add the-u USERNAME (such as crontab-e-U USERNAME) after the corresponding command. The default editor for the crontab file is VI, and you can enter export visual= ' editor ' to change the default editor.
Cron service every minute not only to read all the files in the/var/spool/cron directory, but also to read the/etc/crontab file once. Configuring this file also allows Cron to perform tasks. Using the crontab command is a configuration of a user-level task, and editing a/etc/crontab file is a configuration of a system-level task.
Syntax description
The following is an example of two cron statements (in the/etc/crontab file). The former is used for nightly backup/etc directories, which run the analog program to process server statistics.
3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
5 * * * Root/usr/local/src/analog-5.32-lh/analog >>/dev/null 2>&1
The following is a description of the fields and fields in the CRON statement:
Field |
Description |
1 |
Minutes (0-59) |
2 |
Hours (2-24) |
3 |
Date (1-31) |
4 |
month (1-12; or English abbreviation Jan, Feb, etc.) |
5 |
Weeks (0-6,0 for Sunday; or word abbreviation sun, Mon, etc.) |
6 |
User name (the user's identity when executing the command) |
7 |
The command to execute (PATH) |
|
|
Now look at the first line:
3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
This statement will run the Tar czf/usr/local/backups/daily/etc.tar.gz/etc command at 3:12 A.M. (03:12) every day . >>/dev/null 2>&1 indicates that all standard output is sent to/dev/null (the Linux Recycle Bin) and the standard error output (2) is sent to the same place (i.e./dev/null) as the standard output (1). Running this line of command will not produce any output.
This statement can become slightly more complex:
6 1 * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
It will run the tar czf/usr/local/backups/daily/etc.tar.gz/etc command on June 13, 15:30, Monday .
The following statements can achieve the same effect:
June Mon * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
If you want to run a program in the 15th minute of every hour as a user, Joey, you can use:
* * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1
where the asterisk (*) is a wildcard character, which means Cron ignores this field.
If you want to run a program every two hours , you can use */2 in the hour field. It will be at 2, 4, 6 o ... 22 O'Clock, run at 24 o '. The specific statements are as follows:
0 */2 * * * Joey/usr/bin/somecommand >>/dev/null 2>&1
You can also use a comma (,) to specify multiple times in a cron statement. For example, if you want to run a program at 15 minutes and 30 minutes per hour , you can use 15, 30 in the minute field:
15,30 * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1
If you want to run a program in the first week of the month (that is, number 1th to 7th) , 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 be on the第1-7 day of the month every two hours of 15 minutes and 30 minutes (02:15,02:30 ... 22:15,22:30, etc.) run the/usr/bin/somecommand command.
If you want to execute a collection of scripts in 16:18 every day , you can put all the scripts you want to execute into a directory (such as/home/username/cron), which you can use:
* * * * root Run-parts/home/username/cron >>/dev/null 2>&1
If you want to save the output of a program, you can replace the >>/dev/null 2>&1 with the >>/home/user/somecommand.log 2>&1.
Here are a few examples to illustrate the problem:
Every morning at 6.
0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root.
Every two hours
0 */2 * * echo "has a break now." >>/tmp/test.txt
Every two hours between 11 o'clock and 8 in the morning, eight in the morning.
0 23-7/2,8 * * * echo "a good Dream:)" >>/tmp/test.txt
Every month, number 4th and Monday to Sunday, three a.m., 11.
0 4 * 1-3 command line
January 1 morning, 4.
0 4 1 1 * command line
Summarize
- View the cron configuration for the current user, using Crontab-l
- Edit the cron configuration for the current user, using CRONTAB-E
- Delete the cron configuration for the current user, using Crontab-r
- View/edit/delete a user's cron configuration as root, add-u USERNAME after the command
- Configure system-level tasks, edit/etc/crontab files
Cron is a daemon that can be used to schedule execution of repetitive tasks based on a combination of time, date, month, and week.
Cron assumes that the system is running continuously. If the system is not running when a task is scheduled, the task will not be executed.
To use the Cron service, you must have the Vixie-cron RPM package installed and you must be running the Crond service. To determine if the package is installed, use the Rpm-q vixie-cron command. To determine whether the service is running, use the/sbin/service crond status command.
The main configuration file for Cron is/etc/crontab, which includes the following lines:
Shell=/bin/bash Path=/sbin:/bin:/usr/sbin:/usr/bin mailto=root #如果出现错误, or have data output, data sent to this account as mail home=/ #使用者运行的路径, this is the root directory. # Run-parts * * * * * Root run-parts/etc/cron.hourly 4 * * * Root run-parts/etc/cron.daily 4 * * 0 root run-parts/etc/cron.weekly 4 1 * * Root run-parts/etc/cron.monthly |
The first four rows are variables that you can use to configure the Cron task runtime environment. The value of the shell variable tells the system which shell environment to use (in this case, the bash shell), and the path variable defines the paths used to execute the command. The output of the cron task is mailed to the user name defined by the MAILTO variable. If the MAILTO variable is defined as a blank string (mailto= ""), the e-mail message is not sent. The home variable can be used to set the home directory to use when executing a command or script.
each line in the/etc/crontab file represents a task, in the form of :
Minute hour day Month DayOfWeek command |
minute-minutes, any integer from 0 to 59
hour-hours, any integer from 0 to 23
day-any integer from 1 to 31 (if a month is specified, it must be a valid date for that month)
Month-month, any integer from 1 to 12 (or use the English abbreviations of the month such as Jan, Feb, etc.)
Dayofweek-week, any integer from 0 to 7, where the 0 or 7 represents Sunday (or use the English abbreviations of the week such as Sun, Mon, etc.)
command-commands to execute (commands can be commands such as Ls/proc >>/tmp/proc, or commands to execute scripts you write yourself. )
In any of these values, an asterisk (*) can be used to represent all valid values. For example, an asterisk in a month value means that the command is executed monthly after other constraints have been met.
The short line between integers (-) specifies an integer range. For example, 1-4 means integers 1, 2, 3, 4.
Specify a list with a series of values separated by commas (,). For example, 3, 4, 6, 8 indicate these four specified integers.
A forward slash (/) can be used to specify the interval frequency. After the range is added/means that the integer can be skipped within the range. For example, 0-59/2 can be used to define every two minutes in the Minutes field. The interval frequency value can also be used with asterisks. For example, the value of */3 can be used in the month field to indicate that the task runs every three months.
The line starting with the pound sign (#) is a comment and will not be processed.
As you can see in the/etc/crontab file, it uses the Run-parts script to perform/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly, and/etc/cron.monthly Scripts in the directory that are executed hourly, daily, weekly, or monthly. The files in these directories should be shell scripts. If you want to add a task to cron, put the script that runs the task (or the connection to the script) in the corresponding directory, and if you remove the Run-parts parameter, you can write the full pathname of a script to run, not the folder name.
The following is an example of/etc/crontab, which runs three times a week (Monday, Wednesday, Friday):
4 * * 1,3,5 root run-parts/etc/cron.weekly
Here is an example of a newsgroup that shows how to automatically send a journal file in e-mail
Reply: Help in Crontab
Sent to: Dean Thompson <[email protected]> Date: 2001-03-03 16:35
Newsgroups:comp.os.linux.admin,comp.os.linux.networking,comp.os.linux.security
> How do I set up in the/etc/crontab file to have daily work log files automatically sent to the mailbox [email protected] </var/log?
You can try the following example
0 0 * * * (/bin/mail [email protected] </var/log/messages) >/dev/null 2>&1
If a cron task needs to be executed according to the schedule instead of hourly, daily, weekly, or monthly execution, it can be added to the/ETC/CRON.D directory. All files in this directory use the same syntax as in/etc/crontab. As follows:
# Record the memory usage of the system every Monday # at 3:30a M in the File/tmp/meminfo 3 * * Mon cat/proc/meminfo >>/tmp/ Meminfo # Run custom Script The first day of every month at 4:10AM 4 1 * */root/scripts/backup.sh |
Users other than the root user can use the Crontab tool to configure cron tasks. All user-defined crontab are saved in the/var/spool/cron directory and executed using the user who created them. To create a crontab project as a user, log on as the user, and then type the CRONTAB-E command to edit the user's crontab using the editor specified by the VISUAL or editor environment variable. The file uses the same format as the/etc/crontab. When a change to crontab is saved, the crontab file is saved according to the user name and written to the file/var/spool/cron/username.
The cron daemon examines changes in/etc/crontab files, etc/cron.d/directories, and/var/spool/cron directories every minute. If a change is found, they will be loaded into memory. This way, you do not have to restart the daemon when a crontab file changes.
/etc/cron.allow and/etc/cron.deny files are used to limit the use of cron. The two formats used by the control file are one user per line. None of the two files allow spaces. If the control file is modified, the cron daemon (crond) does not have to be restarted. Using control files is read every time a user adds or deletes a cron task.
The root user can always use cron regardless of what is specified in the control file.
If the Cron.allow file exists, only the users listed in it 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 prohibited from using cron.
To start the Cron service, use the/sbin/service crond Start command. To stop the service, use the/sbin/service crond Stop command. It is recommended that you start the service at boot time.
Increased cron work
When we decide to execute a command on the system on a regular basis, we must define the command to be executed as a cron job, define the cron work to be set up directly in the crontab settings file, set the timing of the command execution, or use cron to perform the/etc/periodically. Cron.* the feature of the directory script, the command to be executed is created as a script document, placed in the directory, and attention to its permissions-must be able to execute!
Reference:http://www.cnblogs.com/youyou/archive/2007/01/20/625365.aspx
Linux Crontab Command Application (RPM)