Location of crontab files

Source: Internet
Author: User
Linux under the corresponding user, with the crontab-l command to see the user scheduled Tasks,-e can be edited, but its real files where it.
Taking CentOS as an example, its true position is: Under/var/spool/cron, a user who performs a scheduled task will have a document named after the user, which is the same as the crontab-l seen with the command.

crontab command

The crontab command is used to install, delete, or list tables used to drive cron background processes. In other words, the user places the sequence of commands to be executed in the crontab file for execution. Each user can have his or her own crontab file. Let's take a look at how to create a crontab file.

crontab files under/var/spool/cron cannot be directly created or modified directly. The crontab file is obtained by the crontab command. Now assume that you have a user name of foxy and you need to create one of your own crontab files. You can start by using any text editor to create a new file, and then write the command you want to run and the time that you want to execute regularly.

Then save the disk and exit. Suppose the file is/tmp/test.cron. After that, use the crontab command to install the file, making it the user's crontab file. Type:

Crontab Test.cron

Such a crontab file is set up. You can go to the/var/spool/cron directory to view, found a more than one foxy file. This file is the required crontab file. View the contents of the file with the more command to find that there are three lines of information in the header:

#DO not EDIT This file-edit the master and reinstall.

# (Test.cron installed on Mon Feb 22 14:20:20 1999)

# (Cron version--$Id: crontab.c,v 2.13 1994/01/17 03:20:37 vivie Exp $)

Probably means:

#切勿编辑此文件-Edit your source file and reinstall it if you need to change it.

#test. cron File installation time: 14:20:20 02/22/1999

If you need to change the contents of the command, you will need to edit the original file again, and then use the crontab command to install it.

Users who can use the crontab command are limited. If the/etc/cron.allow file exists, only the users listed therein can use the command; If the file does not exist In the case where the Cron.deny file exists, only users not listed in the file will be able to use the crontab command, and if two files do not exist, it depends on the settings of some parameters, either to allow Superuser to use the command, or to use it for all users.

The syntax format for the crontab command is as follows:

crontab [-u user] File

crontab [-U user]{-l|-r|-e}

The first format is used to install a new crontab file, install the file referred to in the Chun-ile, and if the "-" symbol is used as the filename, that means using standard input as the source of the installation.

-U If you use this option, you specify which specific user's crontab file will be modified. If you do not specify this option, Crontab will default to the operator's own crontab, that is, the crontab file of the user executing the crontab command will be modified. Note, however, that if you use the SU command to use the crontab command again, there is a good chance of confusion. So if you're using the SU command, it's best to use the-u option to specify which user's crontab file.

-L Displays the current crontab on the standard output.

-R Deletes the current crontab file.

-e Edits the current crontab file using the editor that the visual or editor environment variable refers to. When an end edit is left, the edited file is automatically installed.

[Example 7]

# crontab-l #列出用户目前的crontab.

6 * * * Date

0 */2 * * * Date

0 23-7/2,8 * * * Date

#

How to enter the command and time that you want to execute in the crontab file. Each row in the file includes six fields, the first five of which are the time when the command was executed, and the last field is the command to be executed. Each field is delimited by a space or tab. The format is as follows:

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

The first is the minute, the second is the hour, the third is the day of the one months, the fourth is the month ordinal of the year, the fifth is the week of the week, and the sixth is the order to be executed. None of these items can be empty and must be filled in. If the user does not need to specify several of these items, then you can use the * instead. Because * is a wildcard character, it can be substituted for any characters, so it can be considered any time, that is, the item is ignored. The legal scope of each item is given in table 4-1.

Table 4-1 The legal scope of the specified time

Time
Legal value

Minute
00-59

Hour
00-23, 00 of which is 12 o'clock night.

Day-of-month
01-31

Month-of-year
01-12

Day-of-week
0-6 of which Sunday is 0


This allows the user to write an unlimited number of lines to the crontab file to complete an unlimited number of commands. Command fields can be written to all commands and symbols that can be written on the command line, and all other time domains support enumeration, which means that many time values can be written to the domain, and a comma is used to separate each of the two time values, as long as any one of those time values executes the command.

In addition to the numbers there are a few special symbols is "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" representing from a number to a number, "," separate several discrete numbers. Several examples:

Every morning at 6.

0 6 * * * echo "Good morning." >>/tmp/test.txt//Note that pure echo does not see any output from the screen because cron emails any output to Root's mailbox.

Every two hours

0 */2 * * echo "Have a break now." >>/tmp/test.txt

Between 11 o'clock and 8 in the morning, every two hours, eight in the morning.

0 23-7/2,8 * * * echo "Have a Good Dream:)" >>/tmp/test.txt

4th a month and a week on Monday to three in the morning 11.

0 4 * 1-3 command line

January 1 morning, 4.

0 4 1 1 * command line


Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If there is an error, or if there is data output, the data is sent to this account as mail
home=///user Run path, this is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of/etc/cron.daily scripts
4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script



Attention to the "run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name.

[Example 8]

5,15,25,35,45,55 16,17,18 * * * command

This means that any day any month, in fact, every day 4 o'clock in the afternoon, 5, 6 points 5 min, min, min, min, Min, and Min, the implementation of the command.

[Example 9] in every Monday, three, five 3:00 system into the maintenance state, reboot the system. The following fields should be written in the crontab file:

* * 1,3,5 shutdown-r +5

Then save the file as Foxy.cron, and then type crontab foxy.cron to install the file.

[Example 10] 10 minutes per hour, 40 minutes to execute the Innd/bbslin in the user directory:

10,40 * * * * Innd/bbslink

[Example 11] bin/account This instruction under the user directory for 1 minutes per hour:

1 * * * * bin/account

[Example 12] executes two instructions (each with; delimited) as shown below in the user directory at 3:20 A.M. every day:

3 * * * (/bin/rm-f expire.ls logins.bad;bin/expire$#@62;expire.1st)

[Example 13] every year in January and April, 4th to 9th 3:12 and 3:55 executed/bin/rm-f expire.1st This instruction and added the result after mm.txt this file (mm.txt file is located in the user's own directory location).

12,55 3 4-9 1,4 */bin/rm-f expire.1st$#@62;$#@62;mm.txt

[Example 14] Let's look at a super user's crontab file:

#Run the ' Atrun ' program every minutes

#This runs anything that's due to run from ' at '. ' at ' or ' atrun '.

0,5,10,15,20,25,30,35,40,45,50,55 * * * */usr/lib/atrun

7 * * * updatedb

8,10,22,30,39,46,54,58 * * * */bin/sync

--This article from: http://blog.sina.com.cn/s/blog_49f8dc400100tzn7.html

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.