Crontab for Linux scheduled tasks

Source: Internet
Author: User
Tags cron script
Cron introduction cron is a very useful tool in UNIX, SOLARIS, and LINUX. Use the cron script to make the cron introduction

Cron is a useful tool in UNIX, SOLARIS, and LINUX. The cron script enables scheduled tasks to run automatically on the system background on a regular basis.

Cron command crontab-e-edit the crontab of the user. the crontab is created when the specified crontab does not exist. Crontab-l-list crontab of the user. Crontab-r-delete the user's crontab. Crontab-u <用户名称> -Specify the name of the user whose crontab is to be set. Linux Crontab format

Table: Crontab field and allowed values (Linux Crontab)

Field Description Allowed value
Minutes The fraction of an hour. 0-59
Hours The hour of the day. 0-23
Date The day of the month 1-31
Month The month of the year. 1-12
Week Day of the week 0-6
Command Command Any commands that can be executed
Cron instance 1. executed at a specific time

The basic usage of cron is to execute a task at a specific time. the following example shows how to execute Full backup shell script at AM, January 1, June 10.Note that the time field is in the 24-hour format. if it is eight o'clock P.M., it is rewritten.

30 08 10 06 * /home/ramesh/full-backup
30-30th minutes 08-am 10-10 06-6th months (January 1, June) *-any day of the Week 2. schedule multiple instances (for example, execute twice a day)

The following incremental backup script is executed twice a day.

Run the command at and every day. run the command at both sides of the comma.

00 11,16 * * * /home/ramesh/bin/incremental-backup
00-0th minutes (hourly hours) four o'clock P.M. am and *-Daily *-Monthly *-any day of the Week 3. let the task be executed only at a specific time (for example, only on workday)

The following example shows how to check the database status from to every day (including Saturday)

00 09-18 * * * /home/ramesh/bin/check-db-status
00-0th minutes (hourly start time) one o'clock P.M.-18-9, two o'clock P.M., three o'clock P.M., four o'clock P.M., five o'clock P.M, six o'clock P.M. *-Daily *-Monthly *-any day of the week

If you only want to perform this operation on workdays

00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
00-0th minutes (hourly start time) one o'clock P.M.-18-9, two o'clock P.M., three o'clock P.M., four o'clock P.M., five o'clock P.M, six o'clock P.M. *-Daily *-1-5-Monday, Tuesday, Wednesday, and Friday (workday) 4. how to View cron scheduled tasks

View scheduled tasks of the current user

ramesh@dev-db$ crontab -l

View the scheduled task of the ROOT user, log on to su-root with root, and then execute crontab-l

root@dev-db# crontab -lno crontab for root

View scheduled tasks of other users

root@dev-db# crontab -u sathiya -l@monthly /home/sathiya/monthly-backup00 09-18 * * * /home/sathiya/check-db-status
5. how to edit a cron scheduled task
ramesh@dev-db$ crontab -e
6. schedule scheduled tasks to be executed every minute

Theoretically, there is no shell to be executed every minute, but the following example gives us a better understanding of crontab.

* *** Command

* Represents all possible values. in addition to directly using *, the following examples are also commonly used.

When */5 is specified in the minute field, it indicates every five minutes. When 0-10/2 is specified in the minute field, it indicates every two minutes in the first ten minutes.

The above example is also used for the other four fields

7. schedule the background to execute scheduled tasks every 10 minutes
*/10 * * * * /home/ramesh/check-disk-space

There are some special examples. you can use keywords to replace the above five fields-reboot, midnight, yearly, hourly

Table: Cron special keywords and its meaning

Keyword Equivalent
@ Yearly 0 0 1 1 *
@ Daily 0 0 ***
@ Hourly 0 ****
@ Reboot Run upon restart
8. run the scheduled task (@ yearly) in the first minute of each year)

The following example will be executed at on January 1, January 1 every year.

@yearly /home/ramesh/red-hat/bin/annual-maintenance
9. execute scheduled tasks every month (@ mothly)

The following example will be executed at on the first day of every month.

@monthly /home/ramesh/suse/bin/tape-backup
10. execute scheduled tasks every day (@ daily)

The following example will be executed at every day.

@daily /home/ramesh/arch-linux/bin/cleanup-logs "day started"
11. execute the scheduled task (@ reboot) at each restart)
@reboot CMD
12. how to use the mail keyword to disable/redirect cron emails

By default, crontab will send an email to the administrator who assigns the scheduled task. to redirect this user, add and update the MAIL

ramesh@dev-db$ crontab -lMAIL="ramesh"@yearly /home/ramesh/annual-maintenance*/10 * * * * /home/ramesh/check-disk-space

If you do not want anyone to receive the email, leave the MAIL empty.

13. how to execute a scheduled task every second

The execution per second cannot be scheduled because the minimum processing unit is minute. on the other hand, there is no reason for us to execute one task per second.

14. PATH variable in cron

The above examples use absolute paths.

If you want to use the relative path, you can add the environment variable in crontab.

ramesh@dev-db$ crontab -lPATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/ramesh@yearly annual-maintenance*/10 * * * * check-disk-space
15. define a cron task from the cron file

In addition to direct editing, you can edit the task in a file before importing

ramesh@dev-db$ crontab -lno crontab for ramesh$ cat cron-file.txt@yearly /home/ramesh/annual-maintenance*/10 * * * * /home/ramesh/check-disk-spaceramesh@dev-db$ crontab cron-file.txtramesh@dev-db$ crontab -l@yearly /home/ramesh/annual-maintenance*/10 * * * * /home/ramesh/check-disk-space

Note that this operation will delete the original cron task

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.