Syntax and Application of crontab scheduled tasks

Source: Internet
Author: User
Tags crontab syntax

Https://mp.weixin.qq.com/s/Oi9hppNQMeFiQo9s-ge79A

Crond is a daemon in Linux that is used to periodically execute a task or wait for processing some events. It is similar to a scheduled task in windows. After the operating system is installed, by default, the service tool is installed and the crond process is automatically started. The crond Process regularly checks whether there are tasks to be executed every minute. If there are tasks to be executed, the task is automatically executed.

Job Scheduling classification in Linux
  1. System Task Scheduling

  2. User Task Scheduling

     

System Task Scheduling

The tasks that the system periodically performs, such as writing cached data to the hard disk and clearing logs. There is a crontab file under the/etc directory, which is the configuration file for system task scheduling.

User Task Scheduling

Tasks that the user regularly performs, such as user data backup and scheduled email reminders. You can use the crontab tool to customize your own scheduled tasks. All user-defined crontab files are saved in/var/spool/cronDirectory. The file name is the same as the user name.

Ii. view the current user's scheduled tasks in crontab syntax and Application
crontab -l*/2 * * * * echo "i am crontab" >> /home/zhang/hello
Edit a scheduled task of the current user

You can modify, delete, and add scheduled tasks in the editing status. Comments are generally used #

crontab -e
Delete a scheduled task of the current user
crontab -r

Note that this command is used because all scheduled tasks of this user are cleared once no reminder is sent.

Cron File Format
* *** Command minute hour day month week command column 1st indicates minute 1 ~ 59. Every minute, use * or */1 to indicate 2nd columns to indicate hour 1 ~ 23 (0 indicates 0 points) column 3rd indicates date 1 ~ 31 column 4th indicates the month 1 ~ 12 column ID No. 0 ~ 6 (0 indicates Sunday) the command to be run in column 6th (the command can be ls/proc>/tmp/proc or other commands, it can also be a command to execute your own script .)

In any of the preceding values, the asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value means that this command is executed every month after other constraints are met.

The short line (-) between integers specifies an integer range. For example, 1-4 means integers 1, 2, 3, and 4.

Specifies a list of values separated by commas. For example, 3, 4, 6, and 8 indicate the four specified integers.

A forward slash (/) can be used to specify the interval. Adding/after the range means that integer can be skipped in the range. For example, 0-59/2 can be used to define every two minutes in the minute field. The interval value can also be used with the star number. For example, the value of */3 can be used in the month field to indicate that a task runs every three months.

The line starting with # Is a comment and will not be processed.

As you can see in the/etc/crontab file, it uses the Run-parts script to execute/etc/cron. hourly,/etc/cron. daily,/etc/cron. weekly and/etc/cron. scripts in the monthly Directory, which are executed hourly, daily, weekly, or monthly accordingly. The files in these directories should be shell scripts.

A cron task can be added to the/etc/cron. d directory if it needs to be executed hourly, daily, weekly, or monthly. All files in this directory use the same syntax as those in/etc/crontab.

Instance

Some examples of crontab files:

30 21 * * * service apache restart

The preceding example indicates restarting Apache at every night.

45 4 1,10,22 * * service apache restart

The preceding example indicates that Apache is restarted at on the 1st, 10th, and 22th every month.

10 1 * * 6,0 service apache restart

The preceding example indicates that Apache is restarted at every Saturday and Sunday.

0,30 18-23 * * * service apache restart

The preceding example indicates that Apache is restarted every 30 minutes between and every day.

0 23 * * 6 service apache restart

The preceding example indicates that Apache is restarted at every Saturday.

* */1 * * * service apache restart

Restart Apache every hour

* 23-7/1 * * * service apache restart

Restart Apache every hour between PM and PM.

0 11 4 * mon-wed service apache restart

Restart Apache on November 4 and every Monday to Wednesday.

0 4 1 jan * service apache restart

Restart Apache at on January 1, January 1

 

Environment Variable

1. Set necessary environment variables in shell. For example, if a shell script executes OK manually, but is configured as a background job, Oracle environment variables cannot be obtained, this is because of crontab environment variables. The crontab environment by default does not contain the environment of the current user in the system. Therefore, you need to add the necessary environment variable settings in the shell script.

2. Try to use full paths for all files to avoid relative paths.

  • Write the global path when the script involves a file path;

  • When Java or other environment variables are used for script execution, use the source command to introduce the environment variables.

Syntax and Application of crontab scheduled tasks

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.