Introduction to the Crontab command for executing scheduled tasks in Centos

Source: Internet
Author: User

Introduction to the Crontab command for executing scheduled tasks in Centos

1. Start and Stop Cron

Since Cron is a built-in service in Linux, you can use the following method to start and close the service:

/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration

2. Cron configuration file

2.1 global configuration file

Crontab contains cron. hourly, cron. daily, cron. weekly, cron. monthly, cron. d and crontab and cron. deny files under the/etc directory.

Cron. daily is a job executed once a day, cron. weekly is a job executed once a week. cron. monthly is a job executed once a month, cron. hourly is a job executed every hour. 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.

The/etc/crontab file is generally as follows:

01 *** root run-parts/etc/cron. hourly
02 4 *** root run-parts/etc/cron. daily
22 4 ** 0 root run-parts/etc/cron. weekly
42 4 1 ** root run-parts/etc/cron. monthly

We can add the required cron job to this file.

The/etc/cron. deny file is used to control which users are not allowed to use Crontab.

2.2 user configuration file

Each user has his own cron configuration file, which can be edited through crontab-e. Generally, after the cron configuration file is saved and exited, the system automatically stores the file in the/var/spool/cron/directory. The file is named after the user name.

The linux cron service reads all the content under/var/spool/cron,/etc/crontab,/etc/cron. d every minute.

3. Cron Command Format

Crontab [-u user] File
Crontab [-u user] {-l |-r |-e}

-U: Specifies a user.

-E: Execute the text editor to set the user (current user or specified user) time table. The specified text editor is vi.
-R: delete a user time table.
-L: list the user time table.

4. Cron File Format

* *** Command
Hour, day, month, and week commands

The 1st column indicates minute 1 ~ 59, represented by * or */1 per minute
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run

5. Cron file usage instructions

5.1 General situation

When f1 is *, the program is executed every minute, when f2 is *, the program is executed every hour, and so on.

* ***/Bin/usershell: Execute/bin/usershell every minute
When f1 is a-B, it indicates that execution is performed from the minute a to the minute B. When f2 is a-B, it indicates that execution is performed from the hour a to the hour B, and so on.

0-12 *****/bin/usershell is executed every minute from 0 to 12 minutes every day/bin/usershell
When f1 is */n, it indicates that execution is performed every n minutes. If f2 is */n, it indicates that execution is performed every n hours, and so on.

**/2 ***/bin/usershell is executed every two hours every day./bin/usershell
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.

* 1, 3, 5, 7 ***/bin/usershell execute/bin/usershell once every 3, 3, 4, 7 every day

5.2. Conflict Logic

The date can be set to monthly or weekly. If two segments conflict, the command of the sixth segment will be executed when any one of the segments matches, for example

"30 4*5", will run on the first and 15th of every month plus every Friday at AM.

5.3. symbol "%"

"%" In the Cron file has the function of "ending command line", "line feed", and "redirection". If the special function of "%" is not required, escape using escape characters.

5.4. @ reboot

This does not need to be understood, in order to achieve the purpose of running after the boot, and only run once, in addition to this, it cannot be achieved through the first five segment settings.

@ Annually.

As for @ yearly, @ monthly, and so on, you can use the preceding five sections to set them.

Then I wroteSmall ExampleThe following test is performed:

UseCrontab-eEdit the content as follows:

*/2 ***** echo "I am crontab">/home/zhang/hello

In this way, I am crontab will be written to the hello file every two minutes.

I wrote a script task myself. sh to run the task directly. sh can be run, but it will run after it is added to crontab. Then I checked the solution on the Internet and finally solved it. Let's take a look at the task I originally wrote. sh script:

#! /Bin/sh

Java-jar offergateway. biz. offer-1.0-SNAPSHOT.jar

Crontab scheduled tasks are written as follows:

* ***/Task. sh

Later, I checked the Internet and said that to run crontab, you must use the absolute path. Then I changed all the paths to the absolute path.

In the crontab execution environment variable, there is no environment variable set by the corresponding user. You need to manually set the environment variable to make it take effect. So I changed the above script:

#! /Bin/sh

./Etc/profile

Java-jar/home/zhang/offergateway. biz. offer-1.0-SNAPSHOT.jar

Crontab is also changed to absolute path:

* *** Sh/home/zhang/task. sh

Haha, it's successful ~~ Happy ~~

From: http://li200429.iteye.com/blog/1608758

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.