Use Crontab to have Linux execute shell scripts at timed intervals

Source: Internet
Author: User

  • 1. Cron Service "Ubuntu environment"
  • 2. Crontab usage
  • 3. Edit the Crontab file
  • 4. Process examples
  • 5. Several examples

In Linux, periodic tasks are typically handled by the Cron daemon . Cron reads one or more configuration files that contain the command line and its invocation time. The cron configuration file is called "crontab" and is shorthand for "cron table".

1. Cron Service "Ubuntu environment"

View cron Status

sudo service cron status

Open cron

Sudo/etc/init.d/cron start

Turn off cron

Sudo/etc/init.d/cron stop

Restart Cron

Sudo/etc/init.d/cron restart

2. Crontab usage

Crontab–e: Modify the crontab file, if the file does not exist, it will be created automatically. (This allows you to open your personal crontab profile in edit mode, then add this line: 0 0 * * */home/linrui/xxxxxxxx.sh

This will run the specified. sh file every morning.

Crontab–l: Displays the crontab file.
Crontab-r: Delete the crontab file.
Crontab-ir: Alerts the user before deleting the crontab file.

Write the command and time that you want to execute in the crontab file, where each row contains six fields, the first five of which are the time that the specified command was executed, and the last domain is the command to be executed. Spaces or tabs are used to separate each field. The format is as follows:

Minute hour day-of-month month-of-year day-of-week commands

Valid values are: 00-59 00-23 01-31 01-12 0-6 (0 is Sunday)

In addition to the numbers there are several special symbols: "*", "/" and "-", ","

    • * represents all numbers within the range of values
    • "/" stands for every meaning, "/5" means every 5 units
    • "-" represents a number to a number
    • "," separate a few discrete numbers

Note: Commands note the following points

    • To write an absolute path if a file exists
    • Even if printing is not displayed on the display, running in the background, it is best to redirect the log
3. Use Vim to edit the crontab file
Editor=vi
Export EDITOR
Crontab-e
4. Description of Cron items

The following is the format of the crontab file:

{minute} {Hour} {Day-of-month} {Month} {Day-of-week} {Full-path-to-shell-script}
O minute: Interval is 0–59
O Hour: Interval is 0–23
O Day-of-month: Interval is 0–31
O Month: The interval is 1–12. 1 was January. 12 was December.
O Day-of-week: Interval is 0–7. Sunday can be 0 or 7.

5. Example explanation
1 0 * * */home/lenovosh/work/bianyi.sh Lumus NEDP

Run the specified bianyi.sh script file at 00:01 every day

* * 1,2,3,4,5/home/lenovosh/work/bianyi.sh Lumus NEDP

Run the specified bianyi.sh script file at 23:59 every weekday, or as follows

* * 1-5/home/lenovosh/work/bianyi.sh Lumus NEDP

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ------------------

*/1 * * * */home/lenovosh/work/bianyi.sh Lumus NEDP

Run the specified bianyi.sh script file every minute

1 * */home/lenovosh/work/bianyi.sh Lumus NEDP

1th number per month 14:10 run the specified bianyi.sh script file

1 0 * * 1,3,5/home/lenovosh/work/bianyi.sh lumus NEDP

Run the specified bianyi.sh script file every Monday, five 00:01

Specifically want to see bianyi.sh (Compilation script) How to achieve, please see the next blog!

Transferred from: http://www.cnblogs.com/wucaiyun1/p/6866730.html

Use Crontab to have Linux execute shell scripts at timed intervals

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.