Linux crontab command formats and examples

Source: Internet
Author: User

Execute */5 every five minutes * * * *

Executes 0 per hour * * * *

Executes 0 0 * * * per day

Weekly Execution 0 0 * 0

Monthly execution 0 0 1 * *

Annual Execution 0 0 1 1 *

For detailed usage, refer to this blog post http://yangqijun.iteye.com/blog/1173016

1. Role
Use the crontab command to modify the crontab configuration file, and then the configuration is performed by the Cron utility at the appropriate time, and the command usage permission is for all users.


2. Format
crontab [-u user] {-l |-r |-e}


3. Main parameters
-E: Executes the text editor to set the time table, the empty text editor is VI
-R: Delete the current schedule table
-l lists the current schedule table.


crontab file is in the format "m H d M d cmd ". where m stands for minutes (0~59), H for hours (0~23), D for Day (1~31), M for month (1~12), d for Day of week (0~6,0 for Sunday). CMD represents the program to be run, it is sent to sh execution, the shell only user, HOME, shell three environment variables.  
Use the Cron service to view the Cron service status with server Crond status and service Crond if not started Start starts it, the cron service is a timed execution service that you can add or edit with the crontab command to perform tasks that need to be timed:  
crontab–u//Set a user's Cron service, which is typically required by the root user when executing this command  
crontab–l//list details of a user cron service  
crontab–r//Delete a user's cron service  
crontab–e// Edit a user's cron service  


crontab filename//with filename as the crontab task list file and load
For example, root to 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 the Cron service, the edited content has some formatting and conventions, input: Crontab–u root–e


To Enter the vi editing mode, edit the content must conform to the following format:
*/1 * * * * ls >>/tmp/ls.txt
Edit the/etc/crontab file and add a line at the end: 5 * * * Root init 6 This configures the system to automatically restart 5:30 A.M. every day.
the rows in the crontab file consist of 6 fields separated by a space or TAB key between the different fields. First 5 fields specify when the command will run
minutes (0-59)
hours (0-23)
Date (1-31)
month (1-12)
Day of the week (0-6, of which 0 stands for Sunday, as if 7 also represents Sunday)
The 6th field is a string to execute at an appropriate time.
Example:
#MIN HOUR Day MONTH DAYOFWEEK COMMAND
#每天早上6点10分
6 * * * Date
#每两个小时
0 */2 * * * Date
#晚上11点到早上8点之间每两个小时, 8 in the morning.
0 23-7/2,8 * * * Date
#每个月的4号和每个礼拜一到礼拜三的早上11点
0 4 * 1-3 date
#1月1日早上4点
0 4 1 1 * Date


add: When using Crontab, It is important to note that the environment variables that are accessible in the run script are not necessarily consistent with the environment variables in the current test environment, and it is safer to set the environment variables yourself in a running script (export)

(1) first to build a file Crond.txt as follows, restart   5:36 A.M. every day;
5   *  *   *   reboot 
(2) upload to/opt directory  
(3) Run command  
CRONTAB/OPT/CROND.TXT 
CRONTAB–L 
Let the configuration file take effect: If you have the configuration file in effect and you have to restart Cron, remember, since the Cron profile under each user is modified, restart the cron server.  

/etc/rc.d/crond restart 


service crond Status View cron service status, If the service Crond start is not started, the cron service is a timed execution, and you can add or edit tasks that need to be performed on a timed basis by using the crontab command

Linux crontab command formats and examples

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.