How to perform tasks regularly under Linux crontab usage instructions __linux

Source: Internet
Author: User
Tags cron script
The way to perform tasks regularly under Linux "not original, personal online collection and tidy up a bit"

In Linux, the task of cycle execution is typically handled by cron this daemon [ps-ef|grep cron]. Cron reads one or more configuration files that contain the command line and the time it was invoked.
The cron configuration file is called "crontab" and is shorthand for "cron table".

One, cron look for profiles 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 file is/var/spool/cron/tom.
Generally one user has at most one crontab file.

/etc/crontab This document is responsible for arranging the maintenance system established by the system administrator and the crontab of other tasks.
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
#.----------------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.
# |  |  |  | |
* * * * * command to be executed

/etc/cron.d/This directory is used to store any crontab files or scripts to be executed.


Iv. Competence
Crontab permission issues to/var/adm/cron/next look, file Cron.allow and Cron.deny exist
Usage is as follows:
1, if two files do not exist, only the root user can use the crontab command.
2. If Cron.allow exists but Cron.deny does not exist, only users listed in the Cron.allow file can use the crontab command, and the root user cannot use crontab if the root is not inside.
3, if Cron.allow does not exist, Cron.deny exists, only users listed in Cron.deny file can not use the crontab command, other users can use.
4. If all two files exist, users listed in the Cron.allow file and not listed in Cron.deny can use crontab, if all two files have the same user,
If the user is in the Cron.allow file, the crontab command can be used if the user is in Cron.allow.


The average user in AIX has 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 ordinary users do not have crontab permissions, to open up the ordinary user's crontab permissions can be compiled

V. Create a cron script
The first step: Write a cron script file named Crontest.cron.
15,30,45,59 * * * * * echo "xgmtest ..." >> Xgmtest.txt says, every 15 minutes, execute a Print command
Step Two: Add timed tasks. Executes the command "Crontab Crontest.cron". Get
Step three: "Crontab-l" to see if a timed task is successful or to detect whether a corresponding Cron script is generated under/var/spool/cron


Six, cron service
Cron is a regular execution tool under Linux that can run jobs without human intervention.
/sbin/service crond Start//boot service
/sbin/service Crond Stop//Off service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
/sbin/service crond Status//view service status



Crontab usage
The crontab command is used to install, delete, or list tables used to drive cron background processes. 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. crontab files under/var/spool/cron cannot be directly created or modified directly. The crontab file was created by the crontab command

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
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 is "*", "/" and "-", ",", * represents all the values in the range of numbers, "/" for each Meaning, "/5" means every 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 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.

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 and 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 a data output, the data is sent to this account as a message home= /

Execute/etc/cron.hourly scripts per hour
* * * * Root run-parts/etc/cron.hourly
Daily execution of scripts within/etc/cron.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 the scripts within/etc/cron.monthly
4 1 * * Root run-parts/etc/cron.monthly

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

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

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

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

1 minutes per hour to execute the bin/account this instruction in the user directory:
1 * * * * bin/account

Execute two instructions (each instruction to; separate) as shown below in the user directory 3:20 A.M. every day:
3 * * * (/bin/rm-f expire.ls logins.bad;bin/expire$#@62;expire.1st)

Each year in January and April, 4th to 9th, 3:12 and 3:55, execute/bin/rm-f expire.1st This instruction and add the results 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







At command to implement timed tasks
If we just want to make a particular task run once, then we need to use the at monitor.
At a similar print process, the task is placed in the/var/spool/at directory and runs at a specified time. The AT command is the equivalent of another shell, which, when run at the time command, sends a command to enter any command or program.

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.