crontab command usage in Linux

Source: Internet
Author: User

Crontab

In Linux, crontab is used to set up regular execution of specified commands, and we can use it to specify something that needs to be duplicated, andthe Linux system user simply adds the sequence of commands that they want to execute periodically to the crontab file. The operating system executes these sequence of commands at the time configured by the user. before adding instructions to the crontab file, you need to check to see if the crontab service is up and powered on automatically:

"View Status"

The Linux system has a lot of planned work on it, so this system service is started by default

You can view the status using Service Crond status, which is what I see in Cenos and I can see that it is running.

If you need to start the Crond service, you can use the service crond Start command to start the services

"Set up boot automatically"

We need to set up the service for boot boot, generally the default setting is OK, we can use NTSYSV to open the service configuration for viewing, as shown in front of the * Representative has been selected, scroll down to see if Crond is selected.

"Command Format"

1. Command format:

crontab [-u user] File

crontab [-u user] [-e |-l |-r]

2. Command function:

With the crontab command, we can execute specified system instructions or shell script scripts at a fixed interval of time. The units of the time interval can be any combination of minutes, hours, days, months, weeks, and more. This command is very useful for periodic log analysis or data backup.

3. Command parameters:

-u User: Used to set a user's crontab service, for example, "-u ixdba" means to set IXDBA user's crontab service, this parameter is usually run by the root user.

File:file is the name of the command file, which indicates that file is the Crontab task list and loaded into crontab. If this file is not specified on the command line, the crontab command will accept the commands typed on the standard input (keyboard) and load them into crontab.

-E: Edits the contents of a user's crontab file. If you do not specify a user, the crontab file for the current user is edited.

-L: Displays the contents of a user's crontab file, or displays the contents of the current user's crontab file if no user is specified.

-r: Deletes a user's crontab file from the/var/spool/cron directory and, if no user is specified, deletes the current user's crontab file by default.

-I: Give a confirmation prompt when deleting a user's crontab file.

By default, the Crond service is available to all users of the system. If you need to restrict the use of the Crond service, you can use the configuration file/etc/cron.allow and/etc/cron.deny, words too literally, Cron.allow is the user list that allows the Crond service, the user in Cron.deny is the opposite

The command format in the crontab file is as follows:

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

That is: "Time- sharing week order ", a total of 6 paragraphs, the first 5 paragraphs are the Times, the last paragraph is you want to carry out regular instructions. Separate each paragraph with a space or tab.

take a look at the range of values for the first 5 paragraphs, date and time :

1, Min [00-59] 2, hour [00-23] 3, Day [01-31] 4, month [01-12]
5, week [0-6]; Note that the 0 here means Sunday

The above instruction uses a special symbol "*", which represents all the numbers in the range of values in the current paragraph, such as "*" on the 3rd paragraph for [01-31], and "*" on the 4th paragraph for [01-12] months.

Special symbols In addition to the "*" Number and "/", "-", ",":

/on behalf of "each" meaning, such as "/5" means every 5 units;
-Used to denote a range, such as "[1-10]" representing from 1 to 10;
, used to represent a number of discrete numbers, such as "5,15,25"

Edit crontab file :

An example; crontab–e.

Then directly edit the action you want to implement, save and exit. Like what

* * 3 echo "Hello new day!" >>/tmp/book

We can now use crontab–l to see what we have just set up. can also be found in the/var/spool/cron directory. The file name is the user name.

In order to see the final effect, I first set an example that can see the effect, as follows:

* * * echo "good night!" >>/tmp/book

The result is as follows: you can see that the MyBook file has been written to good night

crontab command usage in Linux

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.