[Linux] regular tasks -crontab

1.1 crond cron job description

Crond linux system is used to periodically execute the command / script or program specified task of a service or software, under normal circumstances, such as after installing centos 6/7 linux operating system, the default will start crond task scheduling service, service crond also regularly (the default is checked every minute) to check whether the system has the task of work to be performed, if there is, it will automatically perform this task timed work according to the rules of its regular tasks set in advance, this service is equivalent to regular tasks crond "alarm" the same.

Timing task 1.2 Linux system

Linux system work scheduling regular tasks can be divided into the following two cases:

First, the task of working linux system itself periodically performed:
task work system periodically performed on its own, such as polling system log, backup system data, clearing the system cache and so on, these tasks we do not need human intervention.

[root@node1 ~]# ls -l /var/log/messages*            # 系统的日志
-rw-------. 1 root root 36080506 9月   1 11:56 /var/log/messages
-rw-------. 1 root root 26616060 8月  11 15:44 /var/log/messages-20190811
-rw-------. 1 root root 26112559 8月  19 10:23 /var/log/messages-20190819
[root@node1 ~]# ls -l /var/log/secure*              # 用户登录日志
-rw-------. 1 root root 10021 9月   1 11:56 /var/log/secure
-rw-------. 1 root root 15821 8月  11 14:44 /var/log/secure-20190811
-rw-------. 1 root root  9131 8月  19 09:51 /var/log/secure-20190819

Second, the work of the task performed by the user:
a user or system administrator tasks on a regular basis to do work, such as at night 0:00 each day cut and backup database data tomcat log, generally these jobs must set your own job by the operation and maintenance.

Example: every night 0:00 to cut the log tomcat

[root@node1 ~]# crontab –l
0 0 * * * /bin/sh /server/scripts/tomcat_backup.sh  >/dev/null 2>&1

Work performed by the user, that is our task to work on operation and maintenance personnel, so the task performed by the user is the focus of this article.

The kind of regular tasks under the 1.2.2 Linux software system

Strictly speaking, regular tasks linux system really a lot, for example: at, crontab, anacron

If: one night to deal with a task, just that day in the evening, it is sudden tasks. To perform at command atd also need to start a service job, in practice, when almost no need to use, so do not explain here.

crontab command
as I said before this command can periodically perform the work tasks.

For example: 0:00 every night database backup data. If you want to execute this command crontab, also need to start a service crond job, this command is most commonly used to produce environment command.

Centos7 crond service system to see if the boot from Kai

[root@node1 ~]# systemctl list-unit-files |grep crond
crond.service                               enabled 

NOTE: centos6 system should use the following commands:

chkconfig  –-list |grep crond

View crond service process

[root@node1 ~]# ps -elf|grep [c]rond
4 S root   633  1  0  80  0 - 31555 hrtime 14:03 ?   00:00:00 /usr/sbin/crond -n

anacron : This command is mainly used for non-7 * 24 hours a day to prepare the server, anacron does not specify a specific time to work on a task, but one day cycle to work or perform tasks in the system after each boot.
It tests should be performed during server downtime, but did not perform work tasks, and perform the task again.

summary:

  1. crond service is running, but is used to manage user crontab timing tasks (rules) command
  2. crond service is commonly used in the production work of important services, at negligible use and anacron
  3. Almost every server will be used crond service

1.3 scheduled task instructions crond

1.3.1 command syntax

crontab 【-u user】 {-l|-e|-r|-i}

View system help

man crontab
[root@node1 ~]# crontab --help
crontab:无效选项 -- -
crontab: usage error: unrecognized option
Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Options:
 -u <user>  define user
 -e         edit user's crontab
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting
 -n <host>  set host in cluster to run users' crontabs
 -c         get host in cluster to run users' crontabs
 -s         selinux context
 -x <mask>  enable debugging

Default operation is replace, per 1003.2

crontab -l to view the current configuration of the user's regular tasks

[root@node1 ~]# crontab –l
0 0 * * * /bin/bash /server/scripts/tomcat_log_cut.sh >/dev/null 2>&1       #tomcat日志切割

crontab -e to enter the current user's regular tasks vim editing mode

[root@node1 ~]# crontab –e
#tomcat日志切割
0 0 * * * /bin/bash /server/scripts/tomcat_log_cut.sh >/dev/null 2>&1
~                                                                             
~

crontab -u user name -l to view the specified user task timing settings

[root@node1 ~]# crontab -u root –l
#tomcat日志切割
0 0 * * * /bin/bash /server/scripts/tomcat_log_cut.sh >/dev/null 2>&1

[root@node1 ~]# crontab -u node1 -l
no crontab for node1

1.3.2 Instructions

We can perform the specified system command or script script at fixed intervals by crontab. Unit time interval can be minutes, hours, days, months, weeks and any combination of the above. (Note: Do a combination of day and week)

crond service can be easily achieved by crontab command log periodic analysis of data backup or business operation and maintenance scenarios work

*/5 * * * * /bin/bash /server/scripts/tomcat_log_cut.sh >/dev/null 2>&1

Description:

Five Star meanings:
: min / 5 on behalf of every 5 minutes
: hours
: Day
: Month
: Day
/ bin / sh /server/scripts/tomcat_log_cut.sh: command or script to be executed (the command must be a full path, you can use " which command "to see the command path)

1.3.3 User permissions and file regular tasks

file Explanation
/etc/cron.deny (refuse) The document listed in the user allowed to use crontab
/etc/cron.allow (allowed) The document takes precedence over the cron.deny (the default does not exist, generally do not)
/ Var / spool / cron / All users crontab file default configuration exists in this directory, the file name with your name
[root@node1 ~]# cat /etc/cron.deny 
node1
[root@node1 ~]# su - node1
[node1@node1 ~]$ crontab -l
You (node1) are not allowed to use this program (crontab)
See crontab(1) for more information
[node1@node1 ~]$ crontab -e
You (node1) are not allowed to use this program (crontab)
See crontab(1) for more information 
[root@node1 ~]#
[root@node1 ~]# ls /var/spool/cron/
root                   #默认是没有的,只有此用户创建了定时任务才有
[root@node1 ~]# cat /var/spool/cron/root 
0 0 * * * /bin/sh /server/scripts/tomcat_log_cut.sh >/dev/null 2>&1     #tomcat日志切割

Table 1.3.4 describes the meaning of command options

parameter meaning
-l (letter) View the contents of the file crontab
-e Edit crontab file contents
-r Delete the contents of the file crontab
-u user Users perform tasks specified use

== particular emphasis on: -r parameter is rarely used in production, also please caution when using, please be sure to rm -rf With the same attitude, can not, absolutely not! ! ! ==

He added:
the crontab -l {|} -e actually operating in such a file / var / spool / cron / current user

The advantage of using crontab command:

  1. crontab can check the syntax
  2. Convenient input

1.3.5 Timing task instruction using format

By default, when the user establishes a timing task rule that record corresponding to the configuration file exists in / var / spool / cron, it crontab configuration file corresponding to the file name is consistent with the login user name, such as: root user Timing task profile to / var / spool / cron / root

The format of crontab scheduled tasks is very simple, user task timing rules are generally divided into six segments (each segment are separated by spaces, the timing of the task system / etc / crontab, is divided into seven sections, divided by spaces) , for the period of five set period of time, command the sixth paragraph is to be executed or the script task segment

[root@node1 ~]#  cat /etc/crontab 
# Example of job definition:
# .---------------- minute (0 - 59)     分钟  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  星期 
# *  *  *  *  * user-name  command to be executed     
  分 时 日 月 周  

1.3.6 crontab syntax meaning a time period in the following table

segment meaning
The first paragraph The minutes
Second paragraph For hours
Third paragraph On behalf of day, day
The fourth paragraph Representing the month
Fifth paragraph Day of the week, day of the week

== Tip: Time memory formulas: the sun and the moon timeshare weeks. The range of formulas, the normal date and time range. ==

1.3.7 crontab syntax of the special symbol table have the following meanings

Special symbols meaning
* Asterisk denotes any time, is in fact "each" means
- Minus sign indicates delimiter indicating a time range, range segment, such as 17:00 to 19:00
, for example: 00 daily tasks 17,18,19 points. 00 17-19 * * * cmd
A comma, a period, a separator means
, for example: daily tasks 5:00 and 10:00, 00 5,10 * * * cmd
/n N represents a number, i.e., "every n units of time." For example: every 10 minutes to perform a task can be written
/ 10 * * * cmd, wherein the range / 10 is 0-59, and therefore can be written as 0-59 / 10

1.3.8 Caption use crontab

image

1.3.9 User Task Timing Example

***** command    每1分钟执行一次command
3,15 ***** command  每小时的第3和第15分钟执行
3,15 8-11 * * * command   在上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * command   每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 command    每个星期一的上午8点到11点的第3和第15分钟执行
22 14 * * 0 command        每周日的14点22分执行
02 04 * * * command        每天4点的02分钟执行

Incidentally 1.4

1.4.1 crontab writing format Description

/dev/null 2>&1

Examples

0 0 * * * /bin/sh  /shell/tomcat_log.sh>/dev/null 2 >&1   

Explanation

0 is input using standard <or <<
1 is a standard output using> or >>
2 is the standard error output using 2> or >> 2
/ dev / null 2> & 1 standard error output that is output to redirect all empty, can written 1> / dev / null 2> / dev / null

The role of redirection

  • Redirected to avoid fragmented files take up empty inode resources
  • Redirect to a specified log in, you can see whether the task execution

1.4.2 write regular tasks Precautions

  1. Add comments for each task, who wrote, what time to write, and what needs to complete
  2. Execute the script with / bin / sh (no execute permissions to prevent the script), the file path to be executed is the absolute path from the root (to prevent File not found)
  3. Try the command to be executed on the script, and then put the script on a scheduled task in. For the regular tasks of the calling script, it can output to standard error output is redirected to empty
  4. The timing belt task %can not be performed, and needs \ escape
  5. If you have value, it must have a value on the minute
  6. Do not use day and week, conflict
  7. >>And >/dev/null 2>&1do not exist

1.4.3 Common Faults and Solutions

A date error

After editing error crontab

crontab: installing new crontab  "/tmp/crontab.QZzQuN":1: bad minute

The reason given: crontab -e command only write, date error or not filled

Solution: crontab -e re-edit regular tasks, correct writing format

Two, inode number is exhausted

When prompted to set up crontab: No space left on device

Locate the fault:

  1. Use df -k to check there is space
  2. Use df -i display / var occupied 100%, if the inode is exhausted, the system will not create a file
  3. In / var / spoo / clientmqueue / super multi-file ls for a long time did not respond
  4. Use rm -rf * will pop root, can be solved using xargs
    cd /var/spool/clientmqueue ls | xargs rm –f

Failure Analysis:
Program the system cron execution of the output content, cron output will be sent contents of the user by mail, and sendmail does not start to produce these documents

Solution: adding back the command crontab inside> / dev / null 2> & 1

Guess you like

Origin www.cnblogs.com/BabySermonizer/p/11444154.html