Linux crontab command details how to use the Linux crontab command

Source: Internet
Author: User
Tags current time ssh

After the operating system is installed, the task scheduling command is started by default.

The crond command periodically checks whether there is any job to be executed every minute. If there is any job to be executed, the job is automatically executed.

Linux task scheduling mainly includes the following two types:

  • 1. Work performed by the system: work to be performed periodically by the system, such as backing up system data and clearing cache

  • 2. Personal work: the work that a user regularly performs, such as checking whether there are new emails on the email server every 10 minutes. This work can be set by each user.

Syntax

Crontab [-u user] file

Or

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

Note:

Crontab is used to allow users to execute programs at a fixed time or interval. In other words, it is similar to the user's time table.

-U user is used to set the time table of the specified user. The premise is that you must have the permission (for example, root) to specify the time table of another user. If-u user is not used, the time table is set.

Parameter description:

  • -E: execute the text editor to set the time table. The preset text editor is VI. If you want to use another text editor, set the VISUAL environment variable to specify the text editor (for example, setenv VISUAL joe)

  • -R: delete the current time table.

  • -L: list the current time table

The time table format is as follows:

F1 f2 f3 f4 f5 program

  • F1 indicates the minute, f2 indicates the hour, f3 indicates the day of the month, f4 indicates the month, and f5 indicates the day of the week. Program indicates the program to be executed.

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

  • 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

  • When f1 is */n, it indicates execution is performed every n minutes. If f2 is */n, it indicates execution is performed every n hours, and so on.

  • When f1 is a, B, c ,... a, B, c ,... execute in minutes. f2 is a, B, c ,... a, B, c... execution in hours, and so on

You can also store all the settings in the file and use crontab file to set the time table.

Instance

Run the Command/bin/ls once every 0th minutes of every hour every month.

0 7 ***/bin/ls

During October 11, December, the/usr/bin/backup task is executed every 20 minutes from to every day.

0 6-12/3*12 */usr/bin/backup

From Monday to Friday, send a letter to alex@domain.name at every day

0 17 ** 1-5 mail-s "hi" alex@domain.name </tmp/maildata

Execute echo "haha" at midnight, 00:20, and 02:20 every month"

20 0-23/2 *** echo "haha"

Let's take a look at several specific examples:

0 */2 ***/sbin/service httpd restart indicates that apache is restarted every two hours.

50 7 ***/sbin/service sshd start means to enable the ssh service at every day

50 22 ***/sbin/service sshd stop means to close the ssh service at every day

0 0 ** fsck/home check/home disk on the 1st and 15th of each month

1 ***/home/bruce/backup: execute the file/home/bruce/backup at the first point of every hour.

00 03 ** 1-5 find/home "*. xxx "-mtime + 4-exec rm {}\; every Monday to Friday three o'clock, in the directory/home, find the file name *. xxx file, and delete the file four days ago.

30 6 */10 ** ls indicates that the ls command is executed at on the first, 11th, 21st, and 31st of each month.

Note: After the program is executed at the specified time, the system will send you a letter showing the program execution content. If you do not want to receive such a letter, add>/dev/null 2> & 1 after each line is empty.

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.