How to use the Linux crontab command

Source: Internet
Author: User

The crontab command is commonly used in UNIX and Linux operating systems to set instructions that are executed periodically . The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution.

In Linux, Linux task scheduling is mainly divided into the following two categories:
1, the system performs the work: the system periodically to perform the work, such as the backup system data, cleans up the cache
2, personal work: a user to do regular work, such as every 10 minutes to check the mail server for new letters, the work can be set by each user

I./etc/crontab,/etc/cron.deny,/etc/cron.allow file Introduction

  The task of the system scheduling is usually stored in the/etc/crontab file, which contains some system running scheduler, through the command we can look inside the content:

  

These tasks will be automatically dispatched after the system is run. /etc/cron.deny and/etc/cron.allow files are also stored in the/etc directory.

  /etc/cron.deny represents a user who cannot use the crontab command

  /etc/cron.allow represents a user who can use crontab.

If two files exist at the same time, then/etc/cron.allow is preferred. If none of the two files exist, then only the root user can schedule the job.

Second, crontab command syntax and its various parameters meaning

The syntax for the crontab command is:

  

  Note:crontab is used to allow a user to execute a program at a fixed or fixed interval, in other words, a user-like time table. -u user refers to the setting of the time table for the specified user, which presupposes that you must have permission (for example, root) to specify another's time schedule. If you do not use the-u user, it means setting your own schedule.

  Description of each parameter:

  

Iii. format of the crontab command

  If we want to create a task scheduler of our own, we can use the crontab-e command, such as:

  

This will go to the VI editing interface, to write the task we want to dispatch, the format of the Crontab Dispatch command is as follows:

  

  

Some common special symbols in the crontab command:

  

Some examples of CRONTAB commands are given below:

  

Iv. creating our own crontab command

  ① Instance One : If I want to write the system's time to the Date1.txt file in the/home directory every minute, enter the following command

If crontab:installing new Crontab appears after saving, the Dispatch command has been successfully
Note : 0 6 * * * echo "Good morning." Note that echo alone doesn't see any output from the screen , because cron emails any output to root's mailbox .
We can go to the/home directory to see if the text file already exists, and view the information inside:

At this point we find that the crontab command we have just written has run normally, and this command will continue as long as there is no terminating command or system shutdown.

② Example Two : if we need to complete two commands simultaneously, the system time is written to the Date1.txt file in the/home directory every minute, and the Date1.txt file is copied to/ A date2.txt file is generated under the directory. What do we usually do then? There are two ways of doing this:

A) The simplest and most straightforward method ( not recommended ): Enter the CRONTAB-E command directly and append a command to the following, such as:

Although this method is relatively simple, it is not recommended because it is not convenient to maintain if there are many commands and if I need different users to execute different commands, here is another method.

b) First write an executable sh file, and then write the task we want to perform in the Sh file, and finally execute our SH file via Crontab ( recommended )

First we write a task.sh file in the/directory, the contents of which are the two commands we have just executed:

Then we enter the crontab-e command, delete the previous two commands, or comment out the previous # number:

V. Some other commands of crontab

  If we need to list what commands we have created for a task schedule, you can use the crontab-l command to view

  

If we need to terminate the task schedule we just created, use the crontab-r command to

  

Vi. Startup of Cron Services

We can see the startup of the Cron service through the chkconfig--list | grep cron command:

  

How to use the Linux crontab command

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.