[Linux] crontab command cron job

crontab regular tasks

I. Introduction

The system may execute instructions or shell scripts at regular intervals by the command crontab

 

Two, crontab configuration file:

Task scheduling of Linux into two categories: system and user task scheduling task scheduling.

Linux system task by cron (crond) system service to control, this system service is enabled by default. Scheduled tasks set by the user's own use crontab command.

View crontab configuration file:

[root@localhost ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- 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

analysis:

  The first line specifies the shell environment used by the system

  Run a second path designated line system

  The third line specifies crond task execution information will be sent via e-mail to the root user, if MAILTO variable is empty, it means not to send information to the user task execution

Note: All user-defined crontab files are stored in / var / spool / cron directory. Consistent with its file name and user name

Three, crontab File Meaning

crontab file created by the user, each row represents a task, each field representing each row of a setting, its format is divided into six fields, set the time period is the period of the first five, the sixth paragraph is command section to be executed

The following format: minute hour day month week command

minute: in the representative fraction of one hour, range 0-59.
hour: represents the first few hours of the day, range 0-23.
mday: represents the first day of the month, range 1-31.
month: the first few months of the year represents the range of 1-12.
week: on behalf of the week, range 0-7 (0 and 7 are Sunday).
who: To what capacity the instruction is executed when you use the crontab -e, do not add this field.
command: command to be executed.

"*" Represents all numbers within the range of values, such as the month field is *, then 1-12 months;

"/" Represents the mean for each predetermined time interval, such as the minutes field * / 10, 1 denotes performed every 10 minutes.

"-" represents an interval from the range, it is a closed interval. The "2-5" indicates "2,3,4,5", fields 0-23 hours / 2 is performed once in every two hours from 0 to 23 points within the range.

"," Dispersion numbers (not necessarily consecutive), such as 1,2,3,4,7,9.

 Four, crontab command Detailed

Format: crontab [-u user] file

crontab [ -u user ] [ -i ] { -e | -l | -r }

  • • -u user: to set a user's crontab services;
  • • file: file name for the command file, said it would file a crontab task list file and loaded into crontab;
  • • -e: edit a user's crontab file content, if not specify a user indicates the current user;
  • • -l: display the contents of a user's crontab file, if not specified users said current user;
  • • -r: delete a user's crontab file from / var / spool / cron directory.
  • • -i: When you delete a user's crontab file to the confirmation prompt.

Five, crontab examples

1) every minute to perform a command (because cron default 1 minute per scan once, so are all * you can)

* * * * * command

3 and 15 minutes execution command 2) per hour

3,15 * * * * command

3) 3 and 15 minutes to perform command 8-11 am each day:

3,15 8-11 * * * command

4) at intervals of 15 minutes and 3 days execution command 2 AM 8-11 points:

3,15 8-11 */2 * * command
0 0 */2 * * command //每隔两天凌晨执行command

5)每个星期一的上午8点到11点的第3和第15分钟执行command

3,15 8-11 * * 1 command

6)每晚的21:30重启smb

30 21 * * * /etc/init.d/smb restart

7)每月1、10、22日的4 : 45重启smb

45 4 1,10,22 * * /etc/init.d/smb restart

8)每周六、周日的1 : 10重启smb

10 1 * * 6,0 /etc/init.d/smb restart

9)每天18 : 00至23 : 00之间每隔30分钟重启smb

0,30 18-23 * * * /etc/init.d/smb restart

10)每一小时重启smb

* */1 * * * /etc/init.d/smb restart

11)晚上11点到早上7点之间,每隔一小时重启smb

* 23-7/1 * * * /etc/init.d/smb restart

12)每月的4号与每周一到周三的11点重启smb

0 11 4 * mon-wed /etc/init.d/smb restart

13)每小时执行/etc/cron.hourly目录内的脚本

0 1 * * * root run-parts /etc/cron.hourly

 

 

 

Guess you like

Origin www.cnblogs.com/HeiDi-BoKe/p/11614278.html