Linux Crontab Instance Analysis _linux

Source: Internet
Author: User
Set every minute to execute my program:
# CRONTAB-E
* */1 * * * */home/lfzhou/vhost/vhost
/sbin/service Crond Stop//Off service
/sbin/service crond Start//boot service
Or
/sbin/service Crond Reload//Reload Configuration

Cron is a regular execution tool under Linux that can run jobs without human intervention. Because Cron is a built-in service for Linux, it does not automatically get up and you can start and close this service in the following ways:
/sbin/service crond Start//boot service
/sbin/service Crond Stop//Off service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
You can also start this service automatically when the system is started:
At the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond Start
Now that the Cron service is in the process, we can use this service, and the Cron service provides the following interfaces for everyone to use:
1. Edit directly with crontab command
The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:
Crontab-u//Set a user's Cron service, which is required by the general root user when executing this command
CRONTAB-L//list details of a user's cron service
Crontab-r//Delete a user's cron service
CRONTAB-E//Edit a user's cron service

For example, root view your cron settings: Crontab-u root-l
Again for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing a cron service, there are some formatting and conventions for editing the content, input: Crontab-u root-e
into the vi editing mode, the contents of the edit must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
The first part of this format is the set of time, the next part is the command to execute, if there are too many commands to execute, you can write these commands into a script, and then call the script here directly, and then remember to write the full path of the command. Time set we have a certain agreement, the front five * number represents five digits, the number of the value range and the meaning is as follows:
Minutes (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Week (0-6)//0 on behalf of Sunday
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. Here are a few examples to illustrate the problem:
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
After each edit of a user's cron settings, Cron automatically generates a file with the same name as this user under/var/spool/cron, and the user's cron information is recorded in this file, which is not directly editable and can only be edited with CRONTAB-E. After Cron starts, read the file every time it is written, and check if you want to execute the commands inside. Therefore, you do not need to restart the Cron service after this file has been modified.
2. Edit/etc/crontab File Configuration cron
The cron service does not only have to read all the files in the/var/spool/cron every minute, but also read the/etc/crontab, so we can configure the file to do something with the cron service. The crontab configuration is for a user, and editing/etc/crontab is a task for the system. The file format for this file is:
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.
--------------------------------------
Basic format:
* * * * command
Time-sharing and Lunar Week command
The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 for 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
The 5th list of the week 0~6 (0 for Sunday)
6th column the command to run
Some examples of crontab files:
* * * */usr/local/etc/rc.d/lighttpd restart
The above example shows a 21:30 reboot lighttpd per night.
4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example represents the 4:45 restart LIGHTTPD for 1, 10, and 22nd of each month.
1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows the 1:10 restart lighttpd every Saturday and Sunday.
0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart
The example above indicates that the LIGHTTPD is restarted every 30 minutes from 18:00 to 23:00 every day.
0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that the 11:00 PM restarts lighttpd every Saturday.
* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart lighttpd every hour.
* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
Between 11 o'clock and 7 in the morning, restart lighttpd every hour.
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month with 11 points per Monday to Wednesday restart LIGHTTPD
0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart
Restart lighttpd at 4 o ' January 1.
Related Article

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.