Crontab: scheduled task execution. Crontab: scheduled task execution.

Source: Internet
Author: User
Tags crontab syntax

Crontab: scheduled task execution. Crontab: scheduled task execution.

I recently came into contact with the needs of scheduled execution programs, so I learned about crontab. This article first introduces the syntax knowledge of crontab, and then makes a demo.

I. crontab syntax

1. Basic crontab format

{Minute} {hour} {day-of-month} {day-of-week} {full-path-to-shell-script}

2. crontab syntax example

1 # At. m runs 2 1 0 ***/root/bin/backup. sh 3 # p. m runs 4 59 11 ** 1, 2, 3, 4, 5/root/bin/backup. sh 5 59 11 ** 1-5/root/bin/backup. sh 6 # Run the command every 5 minutes 7 */5 */root/bin/check-status.sh 8 # the first day of each month p. m run 9 10 13 1 **/root/bin/full-backup.sh 10 #11 p per workday. m run 11 0 23 ** 1-5/root/bin/incremental-backup.sh

Ii. crontab instance

1. Start and Stop crontab

1 service crond start/stop/restart

2. Add a crontab task in crontab-e mode

1 crontab-e # Add Task 2 crontab-l # view all tasks 3 crontab-r # delete a task

Preparations:

Vim/home/helloworld. py

Vim/home/helloworld. sh

1 #helloworld.py2 import time3 with open('test.txt','w+') as f:4     f.write(str(time.time()))5 6 7 #helloworld.sh8 cd /home/helloworld/9 python /home/helloworld/helloworld.py

 

Step 1: run the crontab-e command to enter the editing mode and add the task to be executed.

*/1 * * * * bash /home/helloworld/helloworld.sh

Step 2: Start the crontab Service

Step 3: View crontab logs

1 tail-f/var/log/cron 2 # view log file 3 May 12 19:11:01 localhost CROND [12824]: (root) CMD (bash/home/helloworld. sh) 4 May 12 19:11:01 localhost CROND [12823]: (root) MAIL (mailed 14 bytes of output but got status 0x007f #012) 5 May 12 19:12:01 localhost CROND [12865]: (root) CMD (bash/home/helloworld. sh) 6 May 12 19:12:01 localhost CROND [12864]: (root) MAIL (mailed 14 bytes of output but got status 0x007f #012) 7 May 12 19:13:01 localhost CROND [12880]: (root) CMD (bash/home/helloworld. sh) 8 May 12 19:13:01 localhost CROND [12879]: (root) MAIL (mailed 14 bytes of output but got status 0x007f #012) 9 May 12 19:14:01 localhost CROND [12895]: (root) CMD (bash/home/helloworld. sh) 10 May 12 19:14:01 localhost CROND [12894]: (root) MAIL (mailed 13 bytes of output but got status 0x007f #012) 11 12 # view the helloworld folder 13 ll-t14 #15 [root @ localhost helloworld] # ll-t16 total usage 1217-rw-r -- 1 root 13 May 12 19:11 test.txt 18-rw-r -- 1 root 59 May 12 16:45 helloworld. sh19-rw-r -- 1 root 94 May 12 16:41 helloworld. py20 [root @ localhost helloworld] # ll-t21 total usage 1222-rw-r -- 1 root 13 May 12 19:12 test.txt 23-rw-r -- 1 root 59 may 12 16:45 helloworld. sh24-rw-r -- 1 root 94 May 12 16:41 helloworld. py25 [root @ localhost helloworld] # ll-t26 total usage 1227-rw-r -- 1 root 13 May 12 19:13 test.txt 28-rw-r -- 1 root 59 may 12 16:45 helloworld. sh29-rw-r -- 1 root 94 May 12 16:41 helloworld. py30 [root @ localhost helloworld] # ll-t31 total usage 1232-rw-r -- 1 root 12 May 12 19:14 test.txt 33-rw-r -- 1 root 59 may 12 16:45 helloworld. sh34-rw-r -- 1 root 94 May 12 16:41 helloworld. py35 [root @ localhost helloworld] #

3. Add a task using vim/etc/crontab

1 # view/etc/crontab 2 vim/etc/crontab 3 4 # Add Task 5 SHELL =/bin/bash 6 PATH =/sbin:/bin:/usr/sbin: /usr/bin 7 MAILTO = "" 8 HOME =/9 10 # For details see man 4 crontabs 11 # Example of job definition: 12 #. ---------------- minute (0-59) 13 # |. ------------- hour (0-23) 14 # |. ---------- day of month (1-31) 15 # |. ------- month (1-12) OR jan, feb, mar, apr... 16 # |. ---- day of week (0-6) (Sunday = 0 or 7) OR sun, mon, tue, w ed, thu, fri, sat17 # | 18 # ***** user-name command to be executed19 */1 ***** root bash/home/helloworld. sh

4. You can monitor repeated tasks in the same way.

 

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.