Linux timed Run command script--crontab__linux

Source: Internet
Author: User
Tags save file cron script

Java EE Development of various types of resources download list, the history of the most complete IT resources, personal collection summary.

In Linux, the task of cycle execution is typically handled by the cron 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 (set shell script):

1,/var/spool/cron/yanggang This directory is stored in each user (including root) crontab task, each task is named after the creator, such as user Tom The crontab task corresponds to the file is/var/spool/cron/Tom

yanggang@barry$ sudo ls-l/var/spool/cron/(or sometimes/var/spool/cron/crontabs/)
-RW-------1 root crontab 1071 2011-09-19 17:20 Root
-RW-------1 Yanggang crontab 1176 2011-09-24 11:07 Yanggang
Generally a user has only one crontab file (e.g. root, Yanggang, etc.), and its corresponding log in/var/spool/mail/root (or/var/spool/mail/yanggang) file


2,/etc/crontab This file is responsible for the system administrator to set up the maintenance system and other tasks of the crontab.
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

For example:

*/5 * * * * Root/usr/libexec/atrun

minute: represents the first minute within an hour, with a range of 0-59.
Hour: represents the first few hours of the day, with a range of 0-23.
Mday: represents the first day of one months, the range 1-31.
Month: represents the first few months of the year, with a range of 1-12.
Wday: on behalf of the week, the range 0-7 (0 and 7 are Sunday).
Who : to use what identity to execute the directive, you do not have to add this field when you use CRONTAB-E.
command: the instruction to be executed.


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



second, the Authority
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


Third, create a cron script first step: Write 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



Four, 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

For example: use sudo to stop and start a service

Yanggang@barry$sudo Service Cron Stop
Cron stop/waiting
yanggang@barry$ sudo service cron start
Cron start/running, Process 7502



v. Usage of crontab
The crontab command is used to install, delete, or list the tables used to drive the cron background process, and the user places the sequence of commands to be executed in the crontab file for execution.
Each user can have their own crontab files,/var/spool/cron crontab files can not be directly created or directly modified, the crontab file is created through the crontab-e command

How to enter the command and time to execute in the crontab file, where each line contains 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 are "*", "/" and "-", ",", * represents all the values in the range of numbers

"/" stands 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= /

every hour (first minute) executes the script within the/etc/cron.hourly
* * * * Root run-parts/etc/cron.hourly


daily (wee 4:02) Execute Script 4 * * * * root run-parts/etc/cron.daily in/etc/cron.daily


every week (early Sunday 4:22) executes/etc/cron.weekly within the script 4 * * 0 root run-parts/etc/cron.weekly


Monthly (number 1th a.m. 4:42) to execute the script in/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:
203 * * * (/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,553 4-91,4 */bin/rm-f expire.1st$#@62;$#@62;mm.txt


Vi. Several issues

crontab Editor:
Enter edit command: CRONTAB-E

# m H Dom Mon Dow command
Shell=/bin/bash
* * * * * cd/home/barry/top800/top10/top10_fruits/&&/top10_all.sh

CTRL + O (write)-"Enter" (save file name)--"Ctrl + X" (exit)

Enter view command: crontab-l

# m H Dom Mon Dow command
Shell=/bin/bash
* * * * * cd/home/barry/top800/top10/top10_fruits/&&/top10_all.sh (recommended)

syntax error:

Syntax Error: "(" unexpected  

See also: Linux-bash Syntax Error

Alternatively, specify SHELL interpreter (SH) when crontab-e: Shell=/bin/bash (see crontab Edit example above)


Path Error:

In/var/spool/crontab/yanggang, the following command was added, prompting/var/spool/mail/yanggang in the log file that the xxx.sh path could not be found

* * * * */home/barry/top800/top10/top10_fruits/top10_all.sh

Or

* * * * * bash/home/barry/top800/top10/top10_fruits/top10_all.sh

This is because you use absolute paths to execute script top10_all.sh in crontab, so other scripts referenced in script top10_all.sh also need to use absolute paths to be crontab to find and execute

How to avoid the complexity of the absolute path settings, as shown in the previous six, a few questions , using the following format:

* * * * * cd/home/barry/top800/top10/top10_fruits/&&/top10_ All.sh (This is the recommended way to go to the directory and then execute the script; otherwise, all other scripts in the execution script need to be added to the absolute path)



Recommended Reference:

Linux timed Run command script (Crontab detailed)

Linux Crontab Scheduled Tasks

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.