Linux basics [Article 6], linux basics Article 6

Source: Internet
Author: User
Tags crontab syntax

Linux basics [Article 6], linux basics Article 6

What is crond about scheduled task crond?

Crond is a service or software used in linux to regularly execute commands or specify program tasks. Generally, after the CentOS5/6 linux operating system is installed, the crond task scheduling service is started by default. The crond service checks the system for tasks to be executed on a regular basis (which is checked every minute by default. If yes, the scheduled task is automatically executed according to its preset scheduled task rules.

The crond service cannot meet special requirements. Generally, scripts are written to the daemon for execution.

Why use crond scheduled tasks?

For example, our database or code programs need to perform full backup at every night, so that they need to perform periodic work every night. This is the linux scheduled task crond. Real-time backup and timed backup.

Linux crond scheduled task

In linux, scheduled task scheduling can be divided into the following two situations:

1. scheduled tasks in linux: tasks that are periodically executed by the system, such as polling system logs, backing up system data, and clearing system caches. These tasks do not require human intervention.

The system automatically polls the configuration path of the task.

Some companies may put it in/etc/corntab.

Crond files

2. Tasks that a user or system administrator performs on a regular basis, such as synchronizing time every five minutes with the server on the Internet, back up website data and database data at every night. Generally, you need to set up the data for each user.

Edit Scheduled task: crontab-e

Display scheduled task: crontab-l

Types of timing task software in linux

Scheduled task software in linux, such as at, crontab, and anacron.

At: it is suitable for executing scheduling task commands that only end once. For example, a task needs to be processed one night. It is only one night and is a sudden task. To execute the at command, you also need to start a service named atd. It is rarely used at work.

Crontab: periodically executes tasks, for example, synchronizing server time every five minutes. To execute the crontab command, you need to start a service crond. This crontab command is most commonly used.

Anacron: this command is mainly used to prepare servers for non-24x7 boot. anacron cannot specify a specific time for task execution, it is a task that is executed every day or after the system starts up. It detects tasks that should be executed during server downtime but does not work, and executes the task again.

Note:

1. The crond service is a running program, while the crontab command is a command used by the user to set scheduled rules.

2. crond is the most common important service in enterprise production. at and anacron are rarely used and can be ignored.

3. Almost every server uses the crond service.

Crontab is a privileged command.

Meaning of special symbols in crontab syntax format

Special symbols

Description

*

* Indicates any time, which means "every. For example, 00 23 *** cmd indicates that the cmd task is executed every week at every month. Note that * Indicates each time bit, and * indicates the value range of the time in the last bit. For example, * indicates the hour is equivalent to 00-23.

-

Minus sign, which indicates a separator, indicating a time range and a range, such as to, every day. 00 17-19 *** cmd. That is, execute at, respectively.

,

Comma (,) indicates the meaning of time periods separated. 30 17,18, 19 ***/bin/sh/scripts/oldboy. sh indicates that the/scripts/oldboy. sh script is executed every day at, and. It can also be used with "-", for example, 30 3-5, 17-19 ***/bin/sh/scripts/oldboy. sh.

/N

N indicates a number, that is, "every n units of time". For example, you can write */10 *** cmd to execute a task every 10 minutes, the range of * In */10 is 0-59, so it can also be written as 0-59/10 *** cmd.

Scheduled crontab Writing Task

Each minute, you are allowed to print your name in pinyin to the/server/log/file named by your own name.

Summary:

1. Add comments to the scheduled task

2. do not end with>/dev/null 2> & 1

3. The targeted directory must exist.

4. The path in the scheduled task must be an absolute path.

5. The crond service must be enabled.

How to debug a scheduled task at work:

1. Increase the frequency of executing the task to debug the task.

Code release: Personal Development Environment; office test environment; IDC room test environment; IDC formal environment (group, gated release)

2. Adjust the system time debugging task (cannot be used directly in the production environment) for 5 minutes.

3. Run the script log to output and debug scheduled tasks.

4. Pay attention to the problems caused by some task commands. (>/Dev/null 2> & 1)

5. Pay attention to timing task faults caused by environment variables.

6. Use the crond scheduled Task Service log to debug the scheduled task (/var/log/cron ).

Crontab:

1. system environment variables.

2. Use absolute paths for scheduled tasks.

3. Add/bin/sh to the script permission issue.

4. Escape the time variable with a backslash \ %. It is best to use a script.

5.>/dev/null 2> & 1 (1>/dev/null 2>/dev/null, &>/dev/null ).

6. Add comments before the scheduled task rules.

7. Use a script program to replace the scheduled command line task.

8. Avoid unnecessary program and command output (tar does not use v ).

9. Switch to the upper-level packaging target of the target directory.

10. Use full paths (related to the identification of environment variables) for program commands in the scheduled task script ).

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.