Using cron timer to execute programs under Linux __linux

Source: Internet
Author: User

cron Basic Knowledge noun explanation

Cron is the service name, Crond is the background process, and crontab is a customized Schedule Task table.

Configuration file directory: CD/ETC/CRON.D

Log file directory: Vim/var/log/cron

/sbin/service crond Start//start service  
/sbin/service Crond stop//off service/sbin/service crond restart  
//restart service  
/sbin/ Service Crond Reload//Reload Configuration
configuration file Basic format

# for details, man 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

Minutes (0-59) hours (0-23) Date (1-31) month (1-12) week (0-6, 0 on behalf of Sunday) Order
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

In any of these values, the asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value means that the command is executed every month after other constraints are met.
A dash (-) between integers specifies a range of integers. For example, 1-4 means integers 1, 2, 3, and 4.
Specify a list with a series of values separated by commas (,). For example, 3, 4, 6, 8 indicate these four specified integers.
The forward slash (/) can be used to specify the interval frequency. Adding/<integer> after a range means that you can skip integers within the range. For example, 0-59/2 can be used to define every two minutes in the minute field. The interval frequency value can also be used with the asterisk. For example, a */3 value can be used in the month field to indicate that a task is run every three months.
The line that starts with the pound sign (#) is a comment and is not processed.

examples

0 1 * * root/home/testuser/test.sh every night 1 o'clock/home/testuser/test.sh */10 * * * * * * root/home/testuser/

Test.sh every 10 minutes/home/testuser/test.sh * * * * * * ROOT/USR/LOCAL/ETC/RC.D/LIGHTTPD Restart the above example means 21:30 restart Apache per night.

4 1,10,22 * * * ROOT/USR/LOCAL/ETC/RC.D/LIGHTTPD Restart the above example shows 1, 10, 22nd 4:45 restart Apache.

1 * * * 6,0 root/usr/local/etc/rc.d/lighttpd Restart the above example shows that every Saturday, Sunday 1:10 restart Apache.

0,30 18-23 * * * * * ROOT/USR/LOCAL/ETC/RC.D/LIGHTTPD Restart the above example indicates that Apache is restarted every 30 minutes between 18:00 and 23:00 every day.

0 * * * 6 root/usr/local/etc/rc.d/lighttpd Restart the above example indicates that the 11:00 PM restarts Apache every Saturday. 0 */1 * * * * root/usr/local/etc/rc.d/lighttpd Restart every hour to restart the Apache * 23-7/1 * * ROOT/USR/LOCAL/ETC/RC.D/LIGHTTPD Resta RT Night 11 o'clock to the morning between 7 points, every hour to restart the Apache 0 4 * mon-wed root/usr/local/etc/rc.d/lighttpd Restart monthly 4th and every Monday to Wednesday of the 11 point restart Apache 0 4 1 ROOT/USR/LOCAL/ETC/RC.D/LIGHTTPD Restart January 1 4 point restart Apache */30 * * * * root/usr/sbin/ntpdate 210.72.145.44 sync one every half hour The next time 
Configure time AlignmentNew profiles under/ETC/CRON.D Ntpdate_cron content as follows
Mailto= "" #for ntpdate */1 * * * * *
root/usr/sbin/ntpdate Timeserverip

Why a timed execution failed

First, the reason for the script: most of the cases, we have to believe in science, believe that computers, not ghosts, is our script problem, this problem leads to Crontab can not execute the probability of more than 70%. Because the program executes to a certain step and causes crontab to terminate execution, I ran into a database error while migrating the code. Causing an inability to access and died there.

Second, the implementation of environmental problems, when we encounter the first case, the general can be manually executed program to kill the problem in the cradle, in general, the master should not make the first mistake. The problem is that when we execute the success manually and crontab can not execute, the author encountered once is the implementation of the environment problems, such as the relevant path of the setup problem. Solution: Execute Source/home/user/.bash_profile

at the top of your code. Third, the system time is incorrect. This problem is best understood, and is also a more common and hidden problem, Solution: Date-s ********

Four, is whether our script has executable permissions. You must ensure that the user executing the script has permission to execute the file change.

Five, the crontab daemon died. This is a rare occurrence, but it is not ruled out, when we can not find other reasons to use. Solution: Restart the process.

Six, the crontab does not execute the problem for a long time, the script is written correctly, but is not executed, the final solution is as follows:
        Crontab-u root/var/spool/ Cron/root
        Crontab The root user is in effect
        service Crond restart
& nbsp       Restart service just fine.
Seventh, Crond did not start

Eighth, Script coding problem, script under window to write, to Linux after the report "Nobelium?!" /bin/bash ", create a new shell script with the VI editor, and save after entering the content.


Note 1 It is important to have an idea that the environment variable needs to be loaded itself, the program does not execute most likely to be missing the necessary environment variables 2 Cron's configuration file the last best to add 1>/dev/null 2>&1 Redirects both standard output and standard error output to

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.