Ubuntu using crontab

Source: Internet
Author: User
Tags linux script command crontab example

The Crond service is typically placed in/etc/init.d/crond so that the Crond service can be started automatically after the system starts.

Users in Linux use the crontab command to configure cron tasks.

Crontab There are cron.d,cron.daily,cron.weekly,cron.monthly,cron.hourly five directories and crontab files in the/etc directory.

CRON.D is a task that the system automatically needs to do on a regular basis, but it is not done by the hour, by day, by week, by month, and then it is placed under this directory.

If it is done by the hour, by day, by week, by month , it can be placed under the corresponding directory.
Cron.hourly is a task that executes once per hour.
Cron.daily is a task that executes once a day
Cron.weekly is a task that executes once a week.
Cron.monthly is a monthly task

Crontab use of Linux under the detailed

crontab command Format :
crontab [-u user] File
crontab [-u user] {-e |-l |-r}

Detailed description of the-u parameter:
If you use this option, you specify that the crontab file that is a specific user will be modified. If you do not specify this option, Crontab will default to the current user's crontab, which means that the crontab file of the user who executes the crontab command will be modified.

Note: If you use the SU command to run the crontab command, it is likely that there will be confusion, so when using the SU command, it is best to use the-u option to specify which user's crontab file.

Crontab General use:

crontab [-u user] file-replaces the current crontab with the specified file
crontab [-U user]-l-List the user's current crontab
crontab [-U user]-e-Edit user's current crontab
crontab [-u user]-r-delete a user's crontab

Special Note : If you use Crontab-r to remove all scheduled tasks (requires special care)

Crontab is to work by reading a crontab file, the system Master profile is crontab, and each line in the/etc/crontab file represents a task, and the crontab file has six domains.

The crontab file is in the format: M H d M D cmd

Each field is separated by a space or TAB key, the first five fields are integers or *, the basic format is as follows (the parentheses are the range of values):

# 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

Time-sharing Weekly command

f1-minutes, any integer from 0 to 59
f2-hours, any integer from 0 to 23
f3-any integer from 1 to 31 (if a month is specified, it must be a valid date for that month)
F4-month, any integer from 1 to 12 (or use the English abbreviations of the month such as Jan, Feb, etc.)
F5-week, any integer from 0 to 7, where the 0 or 7 represents Sunday (or use the English abbreviations of the week such as Sun, Mon, etc.)
Command-represents the task you want to perform (you can run a Linux system command, or you can execute a Linux script command that you write yourself.) )

* The role of:
An asterisk (*) can be used to represent all valid values. For example: The asterisk in the month value indicates that the command is executed monthly after other constraints have been met.
When F1 is * indicates that every minute the command,f2 is executed, the task is performed every hour, and the rest of the analogy

The effect of short-term (-) between integers:
When the F1 is a-B, it is executed from the time of the minute to the minute of the hour, and the F2 is a-B indicating that it is executed from the first and the other, and the other analogy

A forward slash (/) between integers can be used to specify the interval frequency:
When F1 is */n, it is executed once every n minutes, F2 is */n for every n-hour interval, and the rest of the analogy

The effect of commas (,) between integers :
When F1 is a, B, C,... A, B, C,... Minutes to execute, F2 for a, B, C,... The B,c is a ... Hours to execute, and the rest of the analogy

Common examples of crontab:

#每天早上7点执行一次/bin/ls:
0 7 * * */BIN/LS

#每天早上6点10分
6 * * * Date

#每两个小时
0 */2 * * * Date

#晚上11点到早上8点之间每两个小时, 8 in the morning.
0 23-7/2,8 * * * Date

#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4 * mon-wed date

#1月份日早上4点
0 4 1 Jan * Date

administrators of Linux systems often use the crontab example :

#在 December, the/usr/bin/backup is performed every 3 hours in the morning from 6 to 12.
0 6-12/3 */usr/bin/backup

#每晚的21:30 Restart Apache.
* * * * */etc/init.d/apache2 restart

#每月1, 10, 22nd, 4:45 restart Apache
4 1,10,22 * */etc/init.d/apache2 restart

#每周六, Sunday 1:10 restart Apache.
1 * * 6,0/etc/init.d/apache2 restart

#在每天18: Restart Apache every 30 minutes between 00 and 23:00.
0,30 18-23 * * */etc/init.d/apache2 restart

#每星期六的11: PM Restart Apache.
0 * * 6/etc/init.d/apache2 restart

#每一小时重启apache
* */1 * * * */etc/init.d/apache2 restart

#晚上11点到早上7点之间, restart Apache every hour
* 23-7/1 * * * */etc/init.d/apache2 restart

#每月的4号与每周一到周三的11点重启apache
0 4 * mon-wed/etc/init.d/apache2 restart

#1月1日的4点重启apache
0 4 1 Jan */etc/init.d/apache2 restart



Measured



11 Point 35 Execute crontab.php file

Ubuntu using crontab

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.