Linux Common Commands--crontab

Source: Internet
Author: User
Tags cron script

crontab 设置定时任务

Syntax format: crontab "option" "File" crontab [options] [file]
Attention:

    1. crontab command and subsequent options and files, there must be at least one space between each element
    2. The newly created cron job will not execute immediately, at least 2 minutes. If you restart Cron, execute it immediately.
    3. Never run Crontab-r. It removes the user's crontab file from the crontab directory (/var/spool/cron). Delete the file, all crontab of the user are deleted.
      Parameter options:
parameter Options Explanatory notes
-L Show scheduled tasks that currently exist
-E For editing timed tasks, the modified content automatically takes effect when you exit the editor
-R Delete a user's crontab file from the/var/spool/cron directory, or delete the current user's crontab file by default if you do not specify a user

The problems should be paid attention to in the timed tasks

Serial Number content
1 Add necessary annotations to a timed task
2 Add/bin/sh before executing shell scripts
3 Timed task command or script end plus >/dev/null 2>&1
4 Timed task commands or programs are best written in script execution
5 Perform the relevant scheduled tasks under the specified user
6 Production task program do not print output information at will. Tar zcvf echo 123 > A.log
7 Timed task execution script to canonical path (/server/scripts)
8 Configuring the Scheduled Task specification operation process
9 The time variable problem is escaped with a backslash \%, preferably with a script

Case one:
File formats for crontab
* * * * * Command-----> Time-of-day Week "commands to execute"

symbols meaning
Minute Represents minutes, which can be any integer from 0 to 59
Hour Represents the hour, which can be any integer from 0 to 23
Day Represents a date, which can be any integer from 1 to 31
Month Represents the month, which can be any integer from 1 to 12
Week Represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents a Sunday
Command The command to execute, either a system command or a script file that you write yourself

Note: In each of these fields, you can also use the following special characters

Special Characters meaning
asterisk (*) Represents all possible values, such as the month field if it is an asterisk, the command action is executed monthly after the other field's constraints are met
Comma (,) You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"
Middle Bar (-) You can use the middle bar between integers to represent an integer range, such as "2-6" for "2,3,4,5,6"
Forward slash (/) You can use a forward slash to specify the interval frequency of the time, such as "0-23/2", which is performed every two hours. A forward slash can be used with asterisks, such as/10, if used in the minute field, indicating that every 10 minutes is performed

Case TWO:
Crond Service Command

Command Description
/sbin/service Crond Start Start the service
/sbin/service Crond Stop Close Service
/sbin/service Crond Restart Restart Service
/sbin/service Crond Reload Reload Configuration
Service Crond Status View Crontab Service Status
Chkconfig–level Crond on Join the Boot auto start

Case THREE:
Examples of common formats

format Description
* * * * * command Execute command every 1 minutes
3,15 * * * command Execution of 3rd and 15 minutes per hour
3,15 8-11 * * command 3rd and 15 minutes from 8 o'clock in the morning to 11.
3,15 8-11 */2 * command 3rd and 15 minutes of every two days from 8 o'clock in the morning to 11.
3,15 8-11 * * 1 command The 3rd and 15 minutes of each Monday 8 o'clock in the morning to 11 are executed.
* * * * */ETC/INIT.D/SMB restart 21:30 Restart SMB per night
4 1,10,22 * */ETC/INIT.D/SMB restart 4:45 restart SMB on 1, 10, 22nd per month
1 * * 6,0/ETC/INIT.D/SMB restart 1:10 restart SMB per Saturday, Sunday
0,30 18-23 * * */ETC/INIT.D/SMB restart Restart SMB every 30 minutes from 18:00 to 23:00 every day
0 * * 6/ETC/INIT.D/SMB restart Every Saturday night, 11:00AM restarts SMB
* */1 * * * */ETC/INIT.D/SMB restart Restart SMB every hour
* 23-7/1 * * * */ETC/INIT.D/SMB restart From 11 o'clock to 7 in the morning, restart SMB every hour.
0 4 * MON-WED/ETC/INIT.D/SMB restart 4th per month with 11-point restart SMB from Monday to Wednesday
0 4 1 Jan */ETC/INIT.D/SMB restart January 1 4-point restart SMB
* * * * * Root run-parts/etc/cron.hourly Execute scripts within the/etc/cron.hourly directory per hour

Case FOUR:
Cleans the message log of system users and ignores log output

Scenario Description: Each task is scheduled to complete, the system will send the task output information by 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, so it is important to redirect each task. For example, you can set the following form in the crontab file, ignoring the log output:

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

"/dev/null 2>&1" means that the standard output is redirected to/dev/null and then the standard error is redirected to standard output, and standard errors are redirected to/dev/null because the standard output has been redirected to/dev/null. This will solve the problem of log output.
Case FIVE:
Create a cron script
The first step: Write the cron script file named Crontest.cron
15,30,45,59 echo "Xgmtest ..." >> Xgmtest.txt, every 15 minutes, executes the print once command
Step Two: Add a scheduled task. Execute the command "Crontab Crontest.cron".
Step three: "Crontab-l" to see if the timed task is successful or if the corresponding cron script is generated under/var/spool/cron
Note:/etc/cron.d/This directory is used to store any crontab files or scripts to be executed

知识扩展

Task scheduling under Linux is divided into two categories: System task scheduling and user task scheduling

System Task Scheduling : The work to be performed by the system periodically, such as writing cache data to hard disk, log cleanup, etc. In the/etc directory there is a crontab file, this is the System Task Scheduler configuration file.

The/etc/crontab file includes the following lines:

SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=""HOME=/51 * * * * root run-parts /etc/cron.hourly24 7 * * * root run-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1 * * root run-parts /etc/cron.monthly

The first four rows are the environment variables that are used to configure the Crond task to run, the shell variable specifies which shell the system will use, this is bash, and the second line of the path variable specifies the path to the System execution command. The third line of the mailto variable specifies that Crond's task execution information will be emailed to the root user, and if the value of the mailto variable is null, the task execution information is not sent to the user, and the home variable in line fourth specifies the home directory to use when executing the command or script.

User Task Scheduling: users to perform regular work, such as user data backup, scheduled email reminders and so on. Users can use the Crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory. Its file name is the same as the user name, and the consumer permissions file is as follows:

/etc/cron.deny the user listed in this file does not allow the crontab command to be used
/etc/cron.allow the users listed in this file are allowed to use the crontab command
/var/spool/cron/directory where all users crontab files are stored, named after the user name

Linux Common Commands--crontab

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.