Linux_ Scheduled Tasks

Source: Internet
Author: User
Tags system log aliyun

What is a scheduled task?

  Tasks and work performed by the Linux system itself on a regular basis:

Rotation system log, backup system data, clean cache, etc.

Var/log/messages        # System log file, Ll/etc/|grep cron          #  Query timed task profile cat/etc/logrotate.d/syslogcat/etc/ logrotate.conf       # Control Log Configuration

Generally speaking, there is no need for tube

  Tasks that the user performs, that is, the tasks and work performed by the operations manager

Types of timing software under Linux system

Timing software: At, Crontab, Anacron

At: For scheduled tasks that end only once

Crontab: Can periodically perform task work

crontab command

-l List View crontab file contents

-e eidit Edit crontab file contents

-r Delete crontab file contents, seldom used

-I delete crontab file contents, will prompt, seldom use

-u Specifies the user to use to perform the task

CRONTAB-E         # equivalent to vi/var/spool/cron/rootcrontab-l          # equivalent to cat/var/spool/cron/root# crontab {-l |-e} is actually in operation/VAR/S pool/cron/files such as the current user
# Edit timed tasks and query set up timed tasks with these two commands

Configure scheduled tasks in two places,/var/spool/cron/directories or/etc/crontab, but it is best not to configure them in/etc/crontab

There are 7 fields in/etc/crontab

Cat/etc/crontab    # View timed task related fields

Minutes, hours, days, months, weeks, and cmd

0-59, 0-23, 01-31, 01-12, 0-7 (0 and 7 = Sunday)

CMD performs a task that contains 2 parts, an absolute path to the command interpreter, and an absolute path to the program

  problem: because the scheduled tasks will be sent to the corresponding user mail, but the mail Service production environment is not open by default, it is possible to generate a large number of small files, a large number of disk-occupied inode, resulting in the disk full but through the df-h and the remaining space phenomenon

  method: Add >/dev/null 2>&1 after the scheduled task, indicating an error or correct output is thrown into the trash bin

Special characters in the crontab

* Means any time, "every" means

Example:

* * * cmd, which indicates that the CMD task is performed daily 23:00 every week

-A minus sign indicates a separation, such as 17-19, that represents 17 18 19:00 per day to perform a task

, comma, meaning of the separation period, 17,18,19 * * * cmd daily 17 18 19:30 perform a cmd task

/n is a number, that is, every n units of time, such as the execution of a task every minute can be written, */10 * * * * cmd, can also be written as 0-59/10 * * * * cmd

3,12 * * * cmd      # daily 3:30 A.M. and 12:30 Noon Perform task 6 * * * cmd        # every 6 hours at the slightest moment of execution * * * cmd             # every night 21:30, perform task 30 8-18/2 * * * cmd       # 8 o'clock in the morning to 18 o'clock in the afternoon every two hours at the slightest moment to perform the task 4 1,10,22 * * cmd    # 1th, 10th, 22nd on the 4:45 A.M. mission XX */1 * * * cmd # every          1 hours per day Perform the task * 23,00-07/1 * * * *       #每天23点 and 0-7 a.m. every one hours every minute to perform this task, this writing is not standard XX * 4 1-3 cmd         # April weekly Monday to Wednesday 11 o'clock in the morning full task # Week and day try not to be the same When used, operation and maintenance of the first principle results predictability, it is recommended to write 0 to do less than 10 of the number

 

Relies on the Crond service

Chkconfig--list|grep ' Crond '        # view the corresponding service run level information # results Crond          0:off1:off2:on3:on4:on5:on6:off# above means 2 3 4 5 class boot all start this service Chkconfig--level 3 Crond        # Let Service run at level 3 chkconfig crond on    # default 2,345-level run # chkconfig crond off    # Cancel 2,345-level run

  

How do I debug timed tasks at work?

1. Increase the frequency of mission execution

Time interval is 1 minutes or 5 minutes after system time

2. Some tasks can not be used in production environment, there is no debugging opportunity

3. Adjust the system Time debugging task for 5 minutes (not for production environment)

4. Debug timed tasks with script log output

5. Commissioning by timed tasks

Note that environment variables lead to timed task failures

For example:

Standard output multiple, multiple standard outputs

The path is not fully written, the command writes the full path or writes the environment variable through the export path.

What is the code, configuration change release process?

Personal development Environment > Office Test Environment > IDC Room Test > IDC Formal Environment (group, grayscale release)

If the script and configuration are all fine, but the scheduled task is not executed, what is the reason?

    1. Whether a script has r x two permissions
    2. Java scripts, notice whether the variables are conflicting with the system environment variables, and the variable is redefined
    3. Whether the script path is a full path and the path is correct

Summary OF Scheduled tasks:

1. Crontab-e for scheduled task editing, check syntax errors, and crontab-l to view the current user's scheduled tasks

2. crontab command to change/var/spool/cron/user files

3. When the scheduled task executes, the current user will be sent a message, if the current user does not start the mail service, will produce a large number of small files occupy the inode space, need to add >/dev/null at any time after each timer 2>&1

4. Command interpreter and command code files for timed tasks, need to give absolute path

5. Any debugging, can not be in the production environment, debugging time Note whether the file has read and Execute permissions, note whether the Java script conflicts with environment variables

6. * Indicates each,-indicates from what to what,/n interval unit time, and tie time period

7. Week and day These two cannot be set at the same time, or increase unpredictability

8. Time-sharing week, in addition to weeks, from small to large

Related timed tasks 

1. Perform/home/shell/collect.pl at 6 3:15 per week and output standard output and standard errors to the/dev/null device

3 * * 06/bin/bash/home/shell/collect.pl >/dev/null 2>&1

2. Crontab in November, every day in the morning 6 to 12, every two hours to perform a/usr/bin/httpd.sh, how to achieve

XX 6-12/2 */bin/bash/usr/bin/httpd.sh >/dev/null 2>&1

  3. Every 5 minutes, get time service through time1.aliyun.com, change the system times

*/5 * * * */bin/bash/usr/sbin/ntpdate time1.aliyun.com >/dev/null 2>&1

  

Linux_ 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.