Crontab commands under "Thunder" Linux are detailed and practical (inductive)

Source: Internet
Author: User

First, preface

The crontab command is used under Linux as a scheduled task for managing and submitting a user's recurring execution, similar to the at scheduled task under Windows. This feature is installed on Linux systems
, the service is installed by default, and the Crond process is automatically started and saved to take effect. Crond periodically checks whether there are tasks to be performed, based on the user's needs.
The task is automatically executed when the condition is reached.

II. Two types of task scheduling

1, system task scheduling: The scheduled work that the system periodically performs, such as log cleanup, caching data to the hard disk, and so on. In the/etc directory, a crontab file is the System Task Scheduler

Configuration file.

2, User task scheduling: User periodically or regularly scheduled work, such as user data backup, timed email reminders, etc., users use the Crontab tool to customize their own

Scheduled Tasks. All user-defined crontab files are stored under the/var/spool/cron directory. Its file name is the same as the user name. Under the ETC directory, there are user permission files:

1)/etc/cron.deny The crontab command is not allowed for users listed in this file
2)/etc/cron.allow the users listed in the file allow the use of the crontab command
3)/var/spool/cron/All users crontab file directory, named after the user name

(The above is the Linux under the crontab of a little explanation and understanding, there are many, long-winded, if not in-depth understanding of the general know that this process is good. Below I was the most intuitive to write out their own on this
The use of task scheduling, I do not like this long-winded, not practical rhetoric. In a word, the system scheduling of the use of etc under that, from the personal play directly crontab-e himself to get a
If you don't want to take it, just comment it out. There are a lot of blog posts on the Internet, but 1000 people have 1000 hamlet, I like such concise expression, this is why I write this blog post of the original, I hope more novice see, can quickly understand and practical, until you really have a discussion of interest can go to look at

The above is a general summary of some crontab knowledge, know above these can be.

Thirdly, the following is a brief and practical analysis of the practical operation model of crontab.

Below, we will explain the following/etc/crontab schedule table.

(Fig. 1-1)

To make it clearer, I intercepted the red-framed part of figure 1-1 above, like 1-2.

(Fig. 1-2)

(Because this text template is not easy to edit these symbols, so you put yourself in the draft of the Come over)

T1 T2 T3 T4 T5 Program (this structure must be remembered, the changes are not in it)
Where T1 is expressed in minutes, T2 represents hours, T3 represents the day of the month, T4 represents the month, and T5 represents the day of the one week. Program represents the procedure (command) to execute.

OK, now let's take a look at the following example in my method:

Format of the Time plan table:
(i), format: T1 * * * * Program
5 * * * * command to execute command every 5 minutes in the minute's schedule
* 5 * * * command to execute commands every 5 hours in the hourly schedule

(b), format: */n * * * * Program
*/5 * * * * command minutes in Schedule (divided by 5), execute command every 5 minutes
* */5 * * * Command Hour schedule, no 5 hours to execute command

(c) Format: A,b,c,d,... * * * * Program
5,15,25,35,45,55 * * * * Command stands for every 5th minute, 15 minutes, 25th minute on the schedule of minutes .... Just start executing the command once.
* * 2,12,22 * * Command represents the number of months in the schedule, each to one months of 2nd, one of the 12th, one months of 22nd will begin to execute a command

(iv) Format: T1 T2 * * * Program
0 0 * * * command to represent the day, the night 0:0 to start the command
5 0 * * * command to represent the night of the day 0:05 to start executing a command

(v) Format: t1-t1+n * * * * Program
5-10 * * * * command represents minutes of the schedule within the 5 minutes to the 10 minutes to execute this command
* 6-7 * * * command to execute this command for the 6th hour to the 7th hour of the schedule on behalf of the hour


1, due to the differences in UNIX version, the use of this */n in HP Unix AIX will be an error. Also note the above five asterisks, the utilization of the five time schedule tables.

2, 5 asterisk "*", the symbol "-" for the duration of the time until, the symbol, "the time interval, cannot use a space (refers to the T1 and T2 cannot use space between, but T1 this time design point inside cannot appear space)

3. Use a space between the asterisk and asterisk to represent a schedule, but the numbers and numbers in the example below are separated by several. This represents only a schedule. Everything is separated by an asterisk as an example. Only asterisks can be separated to represent one schedule table and another schedule table.

Iv. intuitive ways to use
1, directly open/etc/crontab This file to add the save effective can be used

2, direct command in crontab process editing time program (recommended this most intuitive and convenient)
Crontab-l View
Crontab-r Delete (rarely used, server stability general situation when no need to directly comment on the good, do not need to delete)

(Information about the execution of the program commands and the processing of the feedback (cron output configuration) This can actually refer to standard input, output, and standard error usage, where the final information of the schedule execution

This parameter is used to leave the stay and how to leave the method. If you want to be able to manage these programs more carefully, let's take a closer look at the following example:

The following general usage:

You do not want to receive feedback when you want your program commands to follow the plan. After each line, add >/dev/null 2>&1
How to use: */5 * * * */usr/local/php/bin/php-f/scrips/csmj_igetui/igetui.php >/dev/null 2>&1

If you want to receive this information (as a programmer, this information is important to determine the script and so on) in each line of empty one plus 2>&1, plus an imported file in front. Where you should put this information.
The right way Is this: 0 * * * */var/sshl/ >>/root/chenghao/kjl.log 2>&1

As for this standard input, output, and standard error, you can refer to the following URLs.


This late I will make up my own about this most intuitive way to use the above two bloggers write is very good, is not intuitive.

V. On-Machine proposal

Finally in the online selection of the majority of examples of the collection after you have read and can look at and understand these uses:

1, every morning 6 points, the execution of orders. and output the results to the file Test.txt.
0 6 * * echo "Good morning." >>/tmp/test.txt #注意单纯echo, no output is visible from the screen because cron emails any output to the root mailbox.

2. In September, every Monday, three, five of 15 points (that is, 3 o'clock in the afternoon) executes the script without returning any information.
* 9 1,3,5 sh/scrips/ >/dev/null

3. Restart Apache every hour
* */1 * * * */usr/local/etc/rc.d/lighttpd restart

4. Restart Apache every hour from 11 o'clock to 7 a.m. in the evening
0 23-7/1 * * */usr/local/etc/rc.d/lighttpd restart

5, 4th each month and every Monday to Wednesday of the morning 11 point restart Apache
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart or write 0 4 * 1-3/usr/local/etc/rc.d/lighttpd restart

6, January 1, 4 points to restart Apache
0 4 1 Jan */usr/local/etc/rc.d/lighttpd restart

7, the daily 0-point Restart Apapche
0 * * * */usr/local/etc/rc.d/lighttpd restart

8, every day 6:10 A.M.
6 * * * Date

9, every two hours
0 */2 * * * Date

10, from 11 o'clock to 8 in the morning, every two hours, 8 in the morning.
0 23-7/2,8 * * * Date

11.4th in each month and 11 in the morning of Monday to Friday three
0 4 * 1-3 date

12, January 1 morning, 4.
0 4 1 1 * Date

13. Restart Apache every 30 minutes between 18 and 23 points per day
*/30 18-23 * * */usr/local/etc/rc.d/lighttpd restart

6. Some problems needing attention in summarizing and using in depth

1, System-level task scheduling and user-level task scheduling
System-level task scheduling is mainly to complete some maintenance operations of the system, user-level task scheduling is mainly to complete the user-defined tasks, you can put the user-level task scheduling to the system-level task scheduling to complete (not recommended), but in turn, the root user's task scheduling operation can be through the "crontab– Uroot–e "To set, you can also write the dispatch task directly to the/etc/crontab file, it is important to note that if you want to define a scheduled restart of the system task, you must put the task into the/etc/crontab file, even under the root user to create a The task of restarting the system regularly is also invalid.

2, the newly created cron job will not be executed immediately, at least 2 minutes before execution. If you restart Cron, it will be executed immediately.

3, when the crontab suddenly failure, you can try/etc/init.d/crond restart solve the problem. Or check the log to see if a job has execution/error tail-f/var/log/cron.

4, do not run crontab-r chaos. It removes the user's crontab file from the crontab directory (/var/spool/cron). All crontab of the user have been deleted, it is best to use the annotation method.

5, in the crontab% is a special meaning, indicating the meaning of the line. If you want to use the words must be escaped \%, such as the frequently used date ' +%y%m%d ' in the crontab will not be executed, should be replaced by the date ' +\%y\%m\%d '.
6, each task is scheduled to execute, the system will send the task output information through e-mail to the current system users, so that the cumulative, log information will be very large, may affect the normal operation of the system, therefore, it is important to redirect each task.

7, sometimes we create a crontab, but this task is not automatic execution, but the manual execution of this task is not a problem, this situation is generally due to the crontab file is not configured environment variables caused.
When defining multiple dispatch tasks in a crontab file, one of the issues that needs special attention is the setting of environment variables, because when we perform a task manually, it is done in the current shell environment, the program can certainly find the environment variable, and the system will not load any environment variables when it automatically executes the task schedule. Therefore, you need to specify all the environment variables that are required for the task to run in the crontab file, so that the system does not have a problem when it executes the Task Scheduler.
Either you add it in the crontab, or you add the environment variable in the script.

"Where there is a mistake, I hope you will give the point and thank you."

Crontab commands under "Thunder" Linux are detailed and practical (inductive)

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: 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.