Linux automatically runs crontab learning notes

Source: Internet
Author: User
Tags tmp file cron script
On the Linux platform, you can write a Cron script to implement the task scheduling function. Execute tasks at a certain frequencyBy default, the crond process is started in Linux. The crond process does not need to be started or closed.
The crond process reads and executes scheduling tasks. You only need to write the corresponding scheduling script to the cron scheduling configuration file.
Cron scheduling files include the following:
  1. Crontab
  2. Cron. d
  3. Cron. daily
  4. Cron. Hourly
  5. Cron. Monthly
  6. Cron. Weekly
If the task is not executed in hourly monthly weekly mode, you can write the corresponding crontab to the crontab or cron. d directory. Example:
Run the script every minute/opt/bin/test-cron.sh
You can create a script echo-date.sh in cron. d
Content is */1 * root/opt/bin/test-cron.sh Run the task at the specified timeYou can also use the AT command to control the running tasks at the specified time, such as: At-F test-cron.sh-V
-F specifies the script file and-V specifies the running time.

Quote: ea946d690b = "lophyxp"] Use
Contab-L & gt; contabs. tmp
Export the contab configuration.
Edit the contabs. tmp file. Add a row in the following format:
Minute hour day month week command
For example
10 3 ** 0, 6 hello
The Hello program is executed at 03:10 every Saturday and Sunday.
15 4 ** 4-6 hello
The hello program is executed from on Thursday to on Saturday.
Then use
Contab contabs. tmp
Command to import the new configuration.
Generally, it is not recommended to directly modify the relevant configuration files under/etc. How to start the cron Process:/Etc/init. d/crond start
Run the command chkconfig -- add crond to start the cron process at startup.Method 2:Add cron To the startup script:# RC-update add vixie-cron defaultCrontab-L # view your taskCrontab-e # edit your taskCrontab-R # Delete the user's crontab contentExample 2:System cron settings:/etc/crontab
Through the/etc/crontab file, you can set the tasks that the system regularly executes. Of course, to edit this file, you must have the root permission 0 7 *** root mpg123 ~ /Wakeupyun
Example of hours, days, months, and Weeks: 0 4 ** 0Root Emerge-- Sync & emerge-uD world # updates the system at every Sunday.
0 2 1 ** rootRm-F/tmp/* # Clear files under/tmp at on the first day of every month
0 8 6 5 * rootMailRobin 0 6, 12, 18 **** root ............... # execute at, and every day ........ 0 */2 * every two hours ***Echo"Have a break now. "> & gt;/tmp/test.txt every two hours from PM to am, am, 8 *** echo "have a good dream :)" & gt; /tmp/test.txt every month and am from Monday to Wednesday of every week 4*1-3 command line AM, January 1, January 1 0 4 1 1 * command lineGains: You can put some frequently-used tasks into it to simplify the workload, such as checking the server running status every Monday, viewing reports, and killing some processes ......

Crontab is a good entry in Wikipedia. Unfortunately, this address cannot be accessed by proxy in China.

Although the introduction of Crontab is everywhere, after reading this entry in detail, we still have some gains. The Crontab name comes from "chronos", an ancient Greek saying "time.

Common traps

Every SA, DBA, or common Unix user may encounter problems when using Crontab for the first time. Common Errors for running Crontab include:

1) A new Cron JOB is created for testing purposes,The interval must exceed two minutes.Otherwise, the JOB cannot be scheduled. If you must ignore the loading configuration time difference between the two minutes, you can restart Cron Daemon.

2) Notes for starting the X Window program from crontab: either initialize "display =: 0.0" before the program or append the parameter -- display: 0.0 after the application.

3) The % in the command must be escaped: \ %. In my opinion, do not include this parameter in the command line. Simply write it to the script and schedule the script.

Actually, I thinkThe most common problem with crontab is that the environment variable is incorrect.. I often see people in the forum asking:Why is my crontab created and not executed?? When creating a cron job, many people like to run it in the command line, because the environment variables are automatically brought in with shell, in crontab, the job cannot be executed because the correct environment variable cannot be found. This small problem is like having a hard time, and I will remember it after a lesson.

A required skill

After each job is executed, the system automatically sends an email to the current system user. It has been a lot of days and may even crash the entire system. Therefore, it is necessary to perform redirection after each job command:>/dev/null 2> & 1. The premise is that the commands in the job need to be properly output, such as appending to a specific log file.

Appendix: the format of crontab is described as follows:

* Comma (',') specifies the list value. For example: ", 8"
* The range value specified by the hyphen ('-') is "1-6", indicating "1, 2, 4, 5, 6"
* Asterisk ('*') indicates all possible values.

In Linux (open-source systems may all be available), "/" is available. in the minute field, */15 indicates execution every 15 minutes. this feature is not available on commercial Unix systems, such as Aix.

# Use the hash sign to prefix a comment 
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
# | | | | | # * * * * * command to be executed

 

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.