Linux Crond timed Tasks

Source: Internet
Author: User

What is the 1th chapter Crond?

Crond is a service or software that a Linux system uses to perform regular commands or specify program tasks. In general, after we install the Centos5/6linux operating system, the Crond Task Scheduler service is started by default. The Crond service checks the system for tasks to be performed on a regular basis (the default is checked every minute). If so, the scheduled task is automatically performed according to its pre-set scheduled task rules. This Crond timed task service is the same as the one we used in the morning.

The 2nd chapter of the Linux system Crond scheduled task scheduling tasks in Linux system can be divided into the following two situations:

1, the Linux system itself regularly perform tasks: the system periodically self-executing tasks, such as polling system logs, backup system data, clean up the system cache, etc., these tasks do not need us human intervention. For example:

[Email protected] ~]# ll/var/log/messages*

-RW-------1 root root 585506 January 21:32/var/log/messages

-RW-------1 root root 495226 December 14:41/var/log/messages-20151213

-RW-------1 root root 501046 December 10:33/var/log/messages-20151220

-RW-------1 root root 333767 December 20:27/var/log/messages-20151230

-RW-------1 root root 332996 January 12:19/var/log/messages-20160110

2, the user to perform tasks: a user or system administrator to do regular work, such as every 5 minutes and the Internet time server time synchronization, every night 0 o'clock back up the site site data and database data, generally these tasks need to be set by the user himself.

[Email protected] ~]# crontab-l

*/5 * * * */usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

#* * * * * echo "YANGRZ" >>/server/log/yangrz

Chapter 3rd Command Description

The Crond service is a running program, and Crontab is the command that the user uses to set the timing rules.

Crontab option

-l list (view user-set Scheduled Tasks)

-e Edit a timed task configuration file

Crontab–e ===vi/var/spool/cron/root

Crontab–l ===cat/var/spool/cron/root

4th Chapter Crontab The meaning of the time period in the syntax format

Paragraph

Meaning

range of values (integers)

First paragraph

Represents minutes

00-59 (xx it could be 0 . )

Second paragraph

Represents hours

00-23

Third paragraph

Represents day, day

01-31

Fourth paragraph

Represents the Month

01-12

Fifth paragraph

Indicates the day of the week or week

0-7 (0 and 7 all represent Sunday)

Hint: Memory formula: Time-sharing week

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

The 5th Chapter crontab the meaning of special symbols in grammatical format

Special symbol

meaning

*

*" means that any time, the actual meaning is "every". For example, * * * cmd indicates that the CMD task is performed 23:00 every day of the week.

Tip: If the scheduled task is up to the hour, dictate it up to daily. The * on the

Sub-position is equivalent to 0-59, which represents each point.

-

Minus, which represents a separator, representing a time fan The interval, such as 17-19 points, each day of the 17,18,19 point of 00 minutes to perform the task. XX 17-19 * * * cmd. It is the meaning of the 17,18,19 point to be executed separately.

,

Comma, indicating the meaning of the separator period. 17,18,19 * * * */bin/sh/scripts/test.sh 17, 18, 19 points per day to execute the/scripts/test.sh script at the slightest moment.

/n

N for numbers, i.e. "per N unit time ", for example: every 10 minutes, a task can be written as */10 * * * * cmd, where */10 means to execute the cmd command every 10 minutes.

The 6th chapter of writing timed tasks several methods:

Essentials 1: Add necessary annotations to timed task rules

Essentials 2: Timed task commands or programs are best written to execute in scripts

Essentials 3: Add/bin/sh before executing the shell Script task

Essentials 4: Timed task command or script end plus >/dev/null 2>&1

&>/dev/null 1>/dev/null 2>/dev/null

Essentials 5: Perform the relevant scheduled tasks under the specified user

Essentials 6: Production task program do not print output information arbitrarily

Tar zcf echo 123 >a.log

Essentials 7: Script executed by timed tasks to canonical path (/service/scripts)

Essentials 8: Configure timed Task specification operation procedures

The 7th chapter about configuring the scheduled task specification operation process

1) first to succeed in the command line operation, and then copy the successful command into the script, in each small link to reduce the chance of error.

2) then test the script, after the test is successful, copy the canonical path of the script into the scheduled task configuration, do not hand knocking.

3) First Test in test environment, then formal environment specification deployment.

Title: Every 2 hours, the system's/etc/service files are packaged and backed up in/tmp (preferably each backed up into a different package)

Burden:/etc/service does not exist, it should be/etc/services

To package a CD to the parent directory of the target content

The frequency of packaging is divided, the package name must be accurate to the point.

Make sure that the command is executed correctly. And then write it in the script (copy it in)

A timed Task command or program is best written to execute in a script.

Test script correctness (how to write a timed task, how to test the command line)

The script is tested correctly, and command-line commands are copied.

* * * * * * tar zcvf/tmp/service-$ (date +%f-%h) tar.gz./services

This is not correct, in the scheduled task configuration file is also escaped, so the command to write to the file, to avoid errors.

Question: Every night 0 o'clock, put/tmp/services.tar.gz bag, delete the day before yesterday.

/var/log/cron*

This is the crontab log, the execution is correct or error, the log inside will have output.

The 8th chapter is the method of debugging the scheduled tasks in the work:

1. Increase the Task Frequency debug task (some tasks can not be used in the production environment without testing opportunities)

2, code release: Personal Development Environment-Office test Environment-IDC Room test Environment-"IDC formal Environment (group, Grayscale Publishing)

3, adjust the system Time debugging tasks (not directly used in production environment), maintain 5 minutes

4. Debug timed tasks with script log output

5, pay attention to some task orders brought about by the problem

6, (*/1 * * * * * * echo "= =" >>/tmp/oldboy.log >dev/null 2>&1)

7, attention to environmental variables caused by the timing task failure (Java)

8. Debug timed tasks via Crond timed task log (/var/log/cron)

9, put the script in the time task inside, many times to take system variables, can not be taken, there will be problems. The system variables can be redefined in the script.

The 9th Chapter crontab the production and application of Scheduled Tasks 10 Proverbs:

1. System environment variable problem

2. Absolute path for timed tasks

3. Scripting permissions issues, plus/bin/sh

4. Time-variant issues are escaped with a backslash \%, preferably with a script

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

6. Comment Before scheduled task rules

7. Using a script instead of a command line timer task

8. Avoid unnecessary program and command output

9. Cut to the top level of the target directory to package the target

10. The program commands in the timed task script try to use the full path (related to the identification of environment variables).

Linux Crond timed 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.