Article Title: Linux scheduled task system Cron entry. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Cron is a linux scheduled execution tool that can run jobs without human intervention. Since Cron is a built-in service in Linux, but it does not automatically get up, you can use the following methods to start and close this service:
/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration
You can also enable the service automatically when the system starts:
Add:
/Sbin/service crond start
Now that the Cron Service is in the process, we can use it. The Cron Service provides the following interfaces for you to use:
1. directly use the crontab command to edit
The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:
Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.
Crontab-l // list the details of a user's cron Service
Crontab-r // Delete the cron service of no user
Crontab-e // edit a user's cron Service
For example, to view your cron settings as root: crontab-u root-l
For another example, root wants to delete fred's cron settings: crontab-u fred-r
When editing the cron service, the edited content has some formats and conventions. Enter crontab-u root-e.
In vi editing mode, the edited content must conform to the following format: */1 ***** ls>/tmp/ls.txt
The first part of this format is the time setting, and the last part is the command to be executed. If there are too many commands to be executed, you can write these commands into a script, then you can directly call this script here. Remember to write the complete path of the command during the call. We have a certain agreement on the time setting. The first five * numbers represent five numbers. The value range and meaning of the numbers are as follows:
Minutes (0-59)
Hour (0-23)
Date (1-31)
Month (1-12)
Week (0-6) // 0 represents Sunday
In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:
Every morning
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
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 following are the fields and fields in the cron statement:
Field description
1 minute (0-59)
2 hours (2-24)
3 date (1-31)
January (1-12; or Jan, Feb, etc)
The day of the week (0-6 or 0 is Sunday, or Sun or Mon)
6 User Name (this user identity is used when executing commands)
7. 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 be executed at 03:12 every day () run the command tar czf/usr/local/backups/daily/etc.tar.gz/etc.> /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.
[1] [2] Next page