The use of crontab under Linux

Source: Internet
Author: User
Tags cron script

In Linux, the tasks performed by the cycle are typically handled by the cron Daemon (ps-ef|grep cron). Cron reads one or more configuration files that contain the command line and its invocation time.
The cron configuration file is called "crontab" and is shorthand for "cron table".

First, cron service
Cron is a timed execution tool under Linux that can run a job without human intervention.
Service Crond Start//Startup services
Service Crond stop//Shutdown services
Service Crond restart//Restart services
Service Crond Reload//Reload Configuration
Service Crond Status//view services statuses

Second, cron find the configuration file in 3 places:
1,/var/spool/cron/This directory is stored in each user including root crontab task, each task is named after the creator, such as Tom built crontab task corresponding to the file is/var/spool/cron/tom. Generally, a user has only one crontab file at most.

/etc/crontab This document is responsible for arranging the crontab of maintenance systems and other tasks developed by the system administrator.

Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/

# for details see Mans 4 Crontabs

# Example of Job definition:
#.----------------Minute (0-59)
# |.-------------Hour (0-23)
# | |.----------DAY of Month (1-31)
# | | |.-------month (1-12) OR jan,feb,mar,apr ...
# | | | |.----DAY of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * * user-name command to be executed

    • Mailto=root: Is it that when the/etc/crontab in this file is wrong, the error message or the message that is displayed on the screen is passed to whom? Because root and no longer user-side to receive the letter, therefore, I usually will this e-mail to my account, so that I always know the status of the system!
    • 0/5* * * * root/bin/sh/data/xxx.sh: Every 5 minutes, as root, with shell command, execute shell script.

Shell script content:


Four,/etc/cron.d/this directory is used to hold any crontab files or scripts to be executed.

Five, permissions (?
Crontab permissions issue to/var/adm/cron/Next, the file Cron.allow and Cron.deny exist
Usage:  
1, if none of the two files exist, Only the root user can use the crontab command.  
2, if Cron.allow exists but Cron.deny does not exist, only the user listed in the Cron.allow file can use the crontab command, and if the root user is not inside, the root user cannot use crontab.  
3, if the Cron.allow does not exist, Cron.deny exists, only the users who are listed in the Cron.deny file cannot use the crontab command, which can be used by other users.  
4, if two files are present, Users who are listed in the Cron.allow file and are not listed in Cron.deny can use crontab, if the same user is in two files, to cron.allow whether the user is in the file, or if there is a user in Cron.allow, you can use the crontab command.  

Normal users in AIX have crontab permissions by default, and if you want to restrict users from using crontab, you need to edit/var/adm/cron/cron.deny 
Hp-unix The default normal user does not have crontab permissions, to let go of the normal user's crontab permissions can be compiled

Six, create cron script
The first step: Write the cron script file named Crontest.cron.
15,30,45,59 * * * * echo "xgmtest ..." >> xgmtest.txt  , every 15 minutes, perform a print once command  
Step Two: Add a scheduled task. Executes the command "Crontab Crontest.cron". Take care of  
Step Three: "Crontab-l" to see if the scheduled task is successful or to detect if the corresponding cron script is generated under/var/spool/cron


Note: This is a direct replacement for the user under the crontab, not the new

Vii. usage of crontab
The crontab command is used to install, delete, or list tables used to drive a cron background process. The user places the sequence of commands that need to be executed in the crontab file for execution.
Each user can have their own crontab file. The crontab file under/var/spool/cron cannot be created directly or modified directly. The crontab file was created with the crontab command

How to enter the command and time to execute in the crontab file. Each row in the file contains six fields, the first five of which are the time the specified command was executed, and the last field is the command to be executed.
Spaces or tabs are used to separate each field. The format is as follows:
Minute hour day-of-month month-of-year day-of-week commands
Legal value 00-59 00-23 01-31 01-12 0-6 (0 is Sunday)
In addition to the numbers there are several special symbols that are "*", "/" and "-", ",", * represent all the values within the range of the number, "/" for each meaning, "/" for each of the 5 units, "-" represents from a number to a number, "," separate several discrete numbers.

-L Displays the current crontab on the standard output.
-R Deletes the current crontab file.
-e Edit the current crontab file using the editor referred to in visual or editor environment variables. When you finish editing, the edited file is automatically installed.

Viii. Examples:
Every morning at 6.
0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root.

Every two hours
0 */2 * * echo "has a break now." >>/tmp/test.txt

11 o'clock to 8 a.m. every two hours and eight a.m..
0 23-7/2,8 * * * echo "a good Dream" >>/tmp/test.txt

Every month, number 4th and Monday to Sunday, three a.m., 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 an error occurs or there is data output, the data is sent to this account as an email home= /

Execute scripts within/etc/cron.hourly per hour
* * * * * Root run-parts/etc/cron.hourly
Execute scripts within/etc/cron.daily daily
4 * * * Root run-parts/etc/cron.daily

Execute scripts within/etc/cron.weekly every week
4 * * 0 root run-parts/etc/cron.weekly

Every month to execute scripts within the/etc/cron.monthly
4 1 * * Root run-parts/etc/cron.monthly

Note: "Run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.

Every day 4 o'clock in the afternoon, 5, 6 points of 5 min, min, min, min, min, min, when the execution of orders.
5,15,25,35,45,55 16,17,18 * * command

Every Monday, three, five 3:00 the system enters the maintenance state and restarts the system.
* * 1,3,5 shutdown-r +5

10 minutes per hour, 40 minutes execute the innd/bbslin command in the user directory:
10,40 * * * * Innd/bbslink

1 minutes per hour execute the bin/account command in the user directory:
1 * * * * bin/account

"Reprint" https://www.cnblogs.com/longjshz/p/5779215.html

The use of crontab under 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.