Use cron and crontab in Ubuntu

Source: Internet
Author: User

The crond service is usually stored in/etc/init. d/crond, so that the crond service can be automatically started after the system starts.

Linux users use the crontab command to configure cron tasks.

Crontab contains cron. d, cron. daily, cron. Weekly, cron. Monthly, cron. Hourly five directories and crontab files under the/etc directory.

Cron. d is a task automatically and periodically required by the system, but it is not executed by hour, by day, by week, by month, so it is placed under this directory.

If yesBy hour, by day, by week, by monthYou can put it under the corresponding directory.
Cron. Hourly is a task executed every hour.
Cron. daily is a daily task.
Cron. Weekly is a task executed once a week.
Cron. Monthly is a task executed once a month.

How to Use crontab in Linux

Crontab command format:
Crontab [-u user] File
Crontab [-u user] {-E |-L |-r}

-U Parameter Details:
If this option is used, the crontab file of a specific user is modified. If this option is not specified, crontab
By default, the crontab of the current operating user is changed, that is, the crontab file of the user who runs the crontab command will be modified.

Note:
If you run the su command and then run the crontab command, it may be confusing. Therefore, when you use the su command, it is best to use the-u option to specify the user's crontab file.

Crontab usage:

Crontab [-u user] File
-Replace the current crontab with the specified file
Crontab [-u user]
-L
-List the current crontab of a user
Crontab [-u user]
-E
-Edit the current crontab of the user
Crontab [-u user]
-R
-Delete a user's crontab.

Special note: If crontab-R is used
All scheduled tasks are deleted (please be careful)

Crontab is used to read a crontab file. The main configuration file of the system is crontab,/etc/crontab.
Each row in the file represents a task. The crontab file has six fields.

The format of the crontab file is m h d m d cmd.

Each field is separated by a space or a tab key. The first five fields are integers or *. The basic format is as follows (the value range in the brackets ):

# Use the hash sign to prefix a comment

# + ------ Minute (0-59)

# | + ----- Hour (0-23)

# | + ---- Day of month (1
-31)

# |
+ --- Month (1-12)

# |
| + -- Day of week (0-7) (Sunday = 0 or 7)

# |
|

F1 F2 F3 F4 F5 command

Hour, day, month, and week commands

F1-minute, any integer between 0 and 59
F2-hour, any integer between 0 and 23
F3-date, any integer between 1 and 31 (if a month is specified, it must be the valid date of the month)
F4-month, any integer from 1 to 12 (or the month can be abbreviated as Jan, Feb, etc)
F5-week, any integer between 0 and 7, where 0 or 7 represents Sunday (or use the abbreviation of the week, such as Sun, mon
And so on)
Command-indicates the task to be executed (you can run Linux system commands, or you can execute Linux script commands that you have compiled yourself .)

* Function:
An asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value indicates that the command is executed every month after other constraints are met.
When F1 is *, the command is executed every minute, when F2 is *, the task is executed every hour, and so on.

The short-term (-) between integers:
When F1 is a-B, it indicates that execution is performed from the minute to the B minute. When F2 is a-B, it indicates from the to B.
Execute in hours, and so on

The forward slash (/) between integers can be used to specify the Interval Frequency:
When F1 is */N, the execution is performed every n minutes. If F2 is */N, the execution is performed every n hours.

The function of Comma (,) between Integers:
When F1 is a, B, c ,... A, B, C ,... Execute in minutes. F2 is a, B, c ,... A, B,
C... Execute in hours, and so on

Common examples of crontab:

# Run/bin/ls at every morning:
0 7 ***/bin/ls

#06:10 every morning
10 6 **** date

# Every two hours
0 */2 * Date

# Every two hours from PM to am, am
0 23-7/2, 8 **** date

# Am on the 4th day of each month and from Monday to Wednesday of each week
0 11 4 * Mon-wed date

# A.m. of July
0 4 1 Jan * Date

Crontab is often used by Linux administrators.:

# Execute/usr/bin/backup every three hours from to every day within January 1, December:
0 6-12/3*12 */usr/bin/backup

# Restart Apache at every night.
30 21 ***/etc/init. d/apache2 restart

# Restart Apache on 4: 45 on the 1st, 10th, and 22nd of every month
45 4, 10, 22 **/etc/init. d/apache2 restart

# Restart Apache every Saturday and Sunday.
10 1 ** 6, 0/etc/init. d/apache2 restart

# Restart Apache every 30 minutes from to every day.
0, 30 18-23 ***/etc/init. d/apache2 restart

# Restart Apache at every Saturday.
0 23 ** 6/etc/init. d/apache2 restart

# Restart Apache every hour
**/1 ***/etc/init. d/apache2 restart

# Restart Apache every hour between PM and PM
* 23-7/1 ***/etc/init. d/apache2 restart

# Restart Apache from every Monday to every Wednesday on the 4th of each month
0 11 4 * Mon-wed/etc/init. d/apache2 restart

# Restart Apache at on January 1, January 1
0 4 1 Jan */etc/init. d/apache2 restart

 

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.