Detailed introduction to crond scheduled tasks in CentOS

Source: Internet
Author: User

Detailed introduction to crond scheduled tasks in CentOS

Directory

1. crond introduction to scheduled tasks...

2. crond scheduled task permission restriction...

3. Crontab usage...

4. Crontab command writing format...

5. Timing server time synchronization...

6. Pay attention to writing scheduled tasks ..

7. debug the scheduled task...

1, Scheduled taskCrondIntroduction

1> types of timing task Software

At is suitable for scheduling commands that only end once. You need to start a backend atd service.

Crontab needs to start a service crond, which is implemented through the crontab command.

Anacron cannot be periodically executed and can only be executed on a daily basis. However, anacron has the following features: tasks that are not executed during Shutdown can be re-executed at next boot.

Note: crontab is the most common scheduled task.

2> crontab operation

After linux is run, the crond task is automatically started at startup. The system checks whether any task is to be executed every minute (Cyclic detection)

By default, you can use crontab to define scheduled tasks. However, you can use the/etc/cron. allow file to restrict permissions.

3> crontab supports two statuses:

A. Directly compile the scheduled task;

B. If the directory is used, all the files in the directory will be scheduled for execution. The scheduled directory can be set in/etc/crontab.

2,CrondScheduled task permission Restriction

Allows you to manage scheduled tasks.

1) deny the use of crontab tasks

Add the username to be denied to/etc/cron. deny in the following format:

Listen

Nobody

Noaccess

Username1

Username2

Username3

.

2) allow users to access crontab tasks

Add the user name to be allowed in/etc/cron. allow in the following format:

Root

Username1

Username2

.

3) regularly execute a file in the directory

Add a directory to/etc/crontab in the following format:

01 ***** rootrun-parts directory

Note: run-parts can be used to define directories for regular execution.

4) Permission restriction Test

1> Add a user

[Root @ test ~] # Useraddtest

[Root @ test ~] # Echo123456 | passwdtest -- stdin # <== create a password in non-interactive mode

Changingpasswordforusertest.

Passwd: allauthenticationtokensupdatedsuccessfully.

2> reject Test

[Root @ test ~] # Cat/etc/cron. deny

Test

[Root @ test ~] #

[Root @ test ~] # Su-test

[Test @ test ~] $ Crontab-e

You (test) arenotallowedtousethisprogram (crontab)

Seecrontab (1) formoreinformation

After a user is added to cron. deny, the user does not have the permission to edit the scheduled task.

3> allow Test

[Root @ test ~] # Cat/etc/cron. allow

Test

[Root @ test ~] # Su-test

[Test @ test ~] $ Crontab-e # <= the user test is successfully edited.

*/1 ***** echosuccessful>/tmp/a. log

[Test @ test ~] $ Crontab-l

*/1 ***** echosuccessful>/tmp/a. log

After adding a user to cron. allow, the user can edit scheduled tasks.

4> clear the allowed users

[Test @ test ~] $ Su-root

Password:

[Root @ test ~] # Echo>/etc/cron. allow

[Root @ test ~] # Su-test

[Test @ test ~] $ Crontab-l # <= The test user does not have the permission to view data.

You (test) arenotallowedtousethisprogram (crontab)

Seecrontab (1) formoreinformation

[Test @ test ~] $ Cat/etc/cron. deny

Test

After cron. allow is cleared, the user is denied by cron. deny,

Conclusion:/etc/cron. allowHigher than/etc/cron. denyConfiguration

3,CrontabUsage

[Root @ yang1data] # crontab -- help # <= Note: crontab-l-e files with the current user name under/var/spool/cron/

Usage: crontab [-uuser] file # <= specifies a user such as a crontab-uyang2-e, edit the crontab under the yang2 directory

Crontab [-uuser] [-e |-l |-r]

(Defaultoperationisreplace, per1003.2)

-E (edituser'scrontab) # <= edit the scheduled task of the current user

-L (listuser 'scrontab) # <= view scheduled tasks of the current user

-R (deleteuser 'scrontab) # <= delete a scheduled task

-I (promptbeforedeletinguser 'scrontab) # <= Delete the content of the crontab file. A message is displayed before deletion.

-S (selinuxcontext)

Note: When you exit the crontab-e editor, you can check the syntax and directly edit/var/spool/cron/root. If a task is added in large batches, append the task with echo.

CronEvery task executed will be recorded in/var/log/cron.In this log file, you can view the command execution status from this file.

4,CrontabCommand writing format

* ***/Bin/sh/scripts/yy. sh

MinuteHourDayMonthWeekCommand and file path

(00-59) (0-23) (1-31) (1-12) (0-6)

Symbol used to indicate the meaning

* Meaning of each Asterisk

-Reduce the number for a period of time, for example, 0017-19 *** cmd: execute a command at seventeen o'clock P.M., and every day.

, Multiple time periods with commas (,), for example, 0010-11, 17-19 *** cmd: Execute the command once every day at a.m., a.m., and a.m..

/Nn indicates a number, which is executed once every n time. For example, */2 ***** cmd executes the command every 2 minutes.

1) Example:

3012-16/2 *** cmd

It is executed every two hours from four o'clock P.M. to every day.

* 23-7/1 ** 25pxd

Note: This is an incorrect command.

The day and week cannot be used at the same time. If the interval is month, the day or week must have a value.

For example, if there is a value on the hour, there must be a value on the minute.

*/1 ***** echoyangrong>/var/log/yy>/dev/null2> & 1

Yangrong is printed to the yangrong1 file every minute, but the command cannot be executed because >>and>/dev/null cannot be used at the same time.

2)/dev/null2> & 1 explanation

0 indicates the use of standard input <or <

1. standard output> or>

2 is the standard error output using 2> or 2>

>/Dev/null2> & 1: The error output and standard output are all redirected to null. You can write 1>/dev/null2>/dev/null.

3) Role of redirection

Redirecting to null can avoid occupying inode resources by fragment files

Redirects to a specified log to check whether the task is executed.

4) analysis on disk inode full

When the scheduled task is successfully executed or fails, the program will send mail to the upper layer. Because the sendmail function is not enabled during normal work, the mail will be stored in the temporary directory/var/spool/clientmqueue, as many tasks are executed, more and more files are generated. When a file is added one day, Nospaceleftondevice is prompted. The main problem is that the spam file occupies a large space, each file occupies an inode node. (Sendmail is not installed in centos6.4 by default, which may not cause this problem ).

Therefore, add/dev/null2> & 1 to the crontab rule.

Note:> and>/dev/null2> & 1 cannot exist at the same time, otherwise it cannot be executed.

5) restart the crond service.

/Etc/init. d/crondstatus # view the current running status

/Etc/init. d/crondrestart # restart

/Etc/init. d/crondreload # smooth restart

5Timing server time synchronization

1) manual synchronization

[Root @ yang1data] # whichntpdate

/Sbin/ntpdate

[Root @ yang1data] #/sbin/ntpdatetime.windows.com

2) automatic synchronization (using scheduled tasks)

# Syncsystimebyyangrongat2013-9-4

*/5 */sbin/ntpdatetime.windows.com>/dev/null2> & 1

Note: If the number of servers in the LAN is large enough (more than 500), you must build ntpserver on the LAN.

6Note:

If you have mastered the following seven points, you will not make mistakes when writing scheduled tasks.

Serial number

Notes

1

Add comments to each task, who wrote the comments, when the comments are written, and what needs to be completed?

2

Execute the script using/bin/sh (to prevent the script from having no execution permission). The path of the file to be executed is the absolute path starting from the root (to prevent files from being found)

3

Try to put the command to be executed in the script, and then put the script in the scheduled task. For scheduled tasks that call scripts, You can redirect standard output error output to null.

4

% Cannot be executed in the scheduled task. \ escape is required.

5

If there is a value on the hour, the value must be on the minute.

6

Do not use the same day or week.

7

>>And>/dev/null2> & 1 do not exist at the same time

Example:

# Backupmysqlbyyangrongat2013-9-4

304 ***/bin/shtar_mysql.sh>/dev/null2> & 1

7Debug scheduled tasks

A task cannot be easily pushed to an online server. You must test the task on the test server.

Serial number

Debugging method

1

Add frequency debugging tasks, for example, once every 2 hours in the production environment and once every 2 minutes on the Testing Machine

2

Speed up time debugging tasks. For example, if the task is executed within one day in the production environment, add the task and modify the device time.

3

Redirect to a log to view the task execution status. One is to add logs after the scheduled task, and the other is to add logs after the script

4

Environment variables may cause problems

5

Locate the problem through logs. Tail/var/log/cron


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.