Linux scheduled to perform tasks crontab command use detailed

Source: Internet
Author: User

A common use in VPS is to perform tasks regularly, and to perform tasks regularly can reduce human workload and improve efficiency. The timing task of a Linux system is controlled by a cron (Crond) system service. The Linux system already has a lot of planned work on it, so this system service is started by default. In addition, because the user can set the scheduled task themselves, the Linux system also provides the user Control Scheduling task command: crontab command.

Introduction to Linux timing tasks

Crond is a daemon that is used by Linux to periodically perform certain tasks or wait to handle certain events, similar to the Scheduled tasks under Windows, when the operating system is installed, the Service tool is installed by default, and the Crond process is started automatically. The Crond process periodically checks to see if there are any tasks to perform, and if there are tasks to perform, the task is automatically performed. The execution of a scheduled task under Linux relies on the daemon.

The task scheduling under Linux is divided into two types: System task scheduling and user task scheduling.


1 System Task Scheduling: The work that the system should perform periodically, such as write cache data to hard disk, log clean, etc. There is a crontab file in the/etc directory, which is the configuration file for system task scheduling. By editing the file, we can set up a scheduled task.

2 User Task scheduling: Users to perform the work on a regular basis, such as user data backup, timed mail reminders. Users can use the crontab command to customize their scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory and have the same file name as the user name.

The crontab command is common in UNIX and unix-based operating systems, and is used to set periodically executed instructions. This command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek chronos (Χρόνος), which is intended to be time.

Typically, the crontab stored instructions are activated by the daemon, the Crond process runs in the background, and every minute checks to see if a scheduled job needs to be performed. This type of assignment is commonly referred to as cron jobs.

Basic usage of the crontab command

1, crontab command options

Crontab-u//Set a user's Cron service, which is required by the general root user when executing this command
CRONTAB-L//list details of a user's cron service
Crontab-r//Delete a user's cron service
CRONTAB-E//Edit a user's cron service
For example, root view your cron settings: Crontab-u root-l

Again for example, Root wants to delete Fred's cron settings: Crontab-u fred-r

2, crontab command special symbol

"*" represents all numbers within the range of values. This needs special attention.
"/" stands for each meaning, such as */10, if used in the minute field, which means that every 10 minutes is performed.
"-" represents a number to a number, such as "2-6" for "2,3,4,5,6"
"," specifies a list range, for example, "1,2,5,7,8,9"
3, crontab command Timing task file format

The file format for the Crontab scheduling task is generally as follows:

Minute Hour Day Month dayofweek command

Minutes hours days month days per week order

Each field represents the following meanings:
Minute the first few minutes of each hour
Hour a few hours a day to perform this task
Day of the month to perform this task
Month a few months of the year to perform this task
DayOfWeek to perform the task on the first day of the week
Command Specifies the program to be executed
In these fields, except for "Command", which must be specified each time, the other fields are optional

Some examples of crontab files:

* * * */usr/local/etc/rc.d/lighttpd restart
The above example represents 21:30 restart Apache per night.

4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example represents the 4:45 reboot of Apache for 1, 10, and 22nd per month.

1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows the 1:10 reboot of Apache every Saturday and Sunday.

0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that Apache is restarted every 30 minutes from 18:00 to 23:00 every day.

0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that 11:00 PM restarts Apache every Saturday.

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

* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart Apache every hour.

0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month with 11 points per Monday to Wednesday restart Apache

0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart
Restart Apache at 4 o ' January 1.


Note: The Run-parts parameter indicates that all executable files in the following directory are executed. Root indicates that executable files are executed in the directory as root.

Add a timed execution task

As described in the first section, new scheduling tasks can be used in two ways:
1 at the command line input: CRONTAB-E and then add the corresponding task, WQ disk exit. The format of the Add Task command is described in more detail in the previous section. This command edits the cron file of the corresponding user under/var/spool/cron, which belongs to the user task scheduling.

2 directly edit/etc/crontab file, namely Vi/etc/crontab, add the corresponding task, belong to system task scheduling.
The Crond process not only reads all the files in/var/spool/cron every minute, but also reads/etc/crontab, so we can configure this file to perform tasks on a regular basis. The crontab configuration is for a user, and editing/etc/crontab is a task for the system. The file format for this file is:

Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin//Environment variables
Mailto=root//If there is an error, or if there is data output, the data is sent to this account as mail
home=///user Run path, this is the root directory
# Run-parts
* * * * root run-parts/etc/cron.hourly//hourly execution
Scripts within the/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of/etc/cron.daily scripts
4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script
0 * * * * root/root/vncrestart_tennfy.sh//hourly vncrestart_tennfy.sh files

In the last act we add our own timed task, that is, to execute the vncrestart_tennfy.sh file every hour, where vncrestart_tennfy.sh must be an executable file.

Attention matters

There are some noteworthy places to use in crontab, and here's a simple introduction.

1, clean up the user mail

Each task scheduled execution completed, the system will send the task output information through e-mail to the current system users, so that the accumulation of log information will be very large, may affect the normal operation of the system, so it is important to redirect each task.
For example, you can set the following form in the crontab file to ignore the log output:

0 */3 * * */usr/local/apache2/apachectl restart >/dev/null 2>&1

The "/dev/null 2>&1" means that the standard output is redirected to the/dev/null first, then the standard error is redirected to the standard output, and the standard error is redirected to the/dev/null because the standard output has been redirected to the/dev/null. This will solve the problem of log output.

2, the Cron job execution time problem

The newly created Cron job will not be executed immediately and will take at least 2 minutes to execute. If you restart Cron, it executes immediately.

Summary

This article describes the common uses of regular Linux tasks, including two ways:

1 system task scheduling, that is, through editing/etc/crontab to add timed tasks;

2 user task Scheduling, that is, through the CRONTAB-E command to add timed tasks. In the shell script writing, we often use the first method to add a timed task.

Related Article

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.