Linux Automatic Execution program cron and crontab

Source: Internet
Author: User
Article Title: Linux Automatic Execution program cron and crontab. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

If you want to execute some tasks on a regular basis, such as clearing disks, deleting expired files, sending emails and reminders, you can use cron to help you.

First, install the tool. Most releases are pre-installed. cron under Gentoo has many implementations. You can use emerge -- search cron to view them. Here vixie-cron is used because the functions are complete, command syntax is also quite common.

 # Emerge vixie-cron

Then start the cron service:

 #/Etc/init. d/vixie-cron start

Don't forget to add cron To the startup script:

 # Rc-update add vixie-cron default

Complete the installation.

System cron settings:/etc/crontab
You can use the/etc/crontab file to set the tasks scheduled by the system. Of course, you must have the root permission to edit the file.

Let's open/etc/crontab to see the cron Syntax:

1 | # for vixie cron
2 | #
3 | # $ Header:/var/cvsroot/gentoo-x86/sys-process/vixie-cron/files/crontab-3.0.1-r4, v 1.1 2005/03/04 23:59:48 ciaranm Exp $
4 | #
5 | #
6 |
7 | # Global variables
8 | SHELL =/bin/bash
9 | PATH =/sbin:/bin:/usr/sbin:/usr/bin
10 | MAILTO = root
11 | HOME =/
12 |
13 | # check scripts in cron. hourly, cron. daily, cron. weekly and cron. monthly
14 | 0 ***** root rm-f/var/spool/cron/lastrun/cron. hourly
15 | 1 3 *** root rm-f/var/spool/cron/lastrun/cron. daily
16 | 15 4 ** 6 root rm-f/var/spool/cron/lastrun/cron. weekly
17 | 30 5 1 ** root rm-f/var/spool/cron/lastrun/cron. monthly
18 | */10 * root test-x/usr/sbin/run-crons &/usr/sbin/run-crons
The above is a default/etc/crontab file, 7 ~ In row 11, the environment variable value is set when the task is executed. MAILTO = root indicates that the error message is sent to root.

The task is described in 14 ~ In Row 18, each row represents a scheduled task, which is divided into three parts:

The first part is a bunch of strange numbers and stars, which describe the execution cycle and time of the task, which will be explained in detail later.

In the second part, all the above files are "root", which describes the users who run the task.

The third part is the complete command to be executed.

The first part is divided into five small parts, which can be numbers or asterisks, representing minutes, hours, dates, months, and weeks. Let's look at an example:

 07* ** Root mpg123 ~ /Wakeupyun
Minutes, days, months, and weeks
The asterisk (asterisk) indicates no restriction. In the preceding example, mpg123 ~ is executed at 07:00 every day ~ /Wakeupyun, put a song and wake yourself up from your sleep ~

The following is the range of each part:

Minutes 0 ~ 59
Hours 0 ~ 23
Date 1 ~ 31
Month 1 ~ 12
Week 0 ~ 6

 

The range of weeks is 0 ~ 6, 0 represents Sunday, 1 ~ 6 represents Monday ~ Saturday.

Let's look at more examples:

 04**0Root emerge -- sync & emerge-uD world# Every Sunday early morning4Point: update the system
021** Root rm-f/tmp /*# Monthly1Early morning2Point: Clear files under/tmp
0865* Root mail robin # Every year5Month6Send a letter to robin every day to wish him a happy birthday.
If I want to execute a command every two minutes, or at, or every day, you can use "/" and "," To set:

*/2* *** Root ...............# Execute every two minutes ........
06,12,18* ** Root ...............# Daily6Point,12Point,18Click execute ........

[1] [2] Next page

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.