linux establish regular tasks

I. Introduction

Regular tasks with shell scripts can achieve a lot with relaxing function. The machine in accordance with the timing of the human will to do something, definitely pleasing thing!

crond is under linux to periodically perform some task or a daemon pending certain events, and scheduled tasks under the windows Similarly, when the installation is complete operating system, installed by default this service tool, and will automatically start crond process, crond process checks per minute on a regular basis whether there are tasks to be performed, if there are tasks to be performed automatically perform this task.
Task scheduling of Linux into two categories, task scheduling system and user tasks scheduling.

** (1) ** system task scheduling
system task scheduling, a global-oriented features. Users without limitation, continued to perform regular tasks.

* (2) * User scheduling
user scheduling, only user-oriented features. Timing task set by the general user, compared to the system there are more tasks are different

** ** user permissions of the file
have two files in the directory etc
cron.deny # listed in the file the user is not allowed to use crontab command
the user allows cron.allow # listed in the file using the command crontab

** storage directory user crontab file *
/ var / spool / cron / # user crontab file stored in directories, with your name

Second, the format

System scheduling tasks generally stored in / etc / crontab file, stored inside a scheduler to run some systems
using the command:

cat /etc/crontab
SHELL=/bin/bash # 第一行SHELL变量指定了系统要使用哪个shell,这里是bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin # 第二行PATH变量指定了系统执行 命令的路径
MAILTO=root # 第三行MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户,,如果MAILTO变量的值为空,则表示不发送任务 执行信息给用户

# 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

Second, set

1, regular tasks View

crontab -l

2, edit regular tasks

crontab -e

3, increasing the regular tasks

* * * * * command

Equivalence

minute hour day month week command

minute: min expressed, may be any integer from 0 to 59.

hour: the hour, may be any integer from 0 to 23.

day: the date, can be any integer from 1 to 31.

month: the month, can be any integer from 1-12.

week: represents the day of the week, it can be any integer from 0 to 7, where 0 or 7 represents Sunday.

Case

Example 1: a command execution every 1 minute

* * * * * command

Example 2: 3 and every hour in the first 15 minutes of

3,15 * * * * command

Example 3: 3 and performed in the first 15 minutes of 8:00 to 11:00

3,15 8-11 * * *command

Example 4: 3 and performs the first 15 minutes of every day 8:00 to 11:00

3,15 8-11 */2  *  * command

Example 5: 3 and 8:00 of 15 minutes to 11 performed weekly

3,15 8-11 * * 1 command

test.sh script at 21:30 every night to perform home directory: Examples 6

30 21 * * * /bin/sh /home/test.sh

Example 7: 1 month, 10, 22 May 4:45 test.sh execute script in your home directory

45 4 1,10,22 * * /bin/sh /home/test.sh

Example 8: every Saturday, Sunday 1:10 test.sh execute script in your home directory

10 1 * * 6,0 /bin/sh /home/test.sh

Example 9:18 per day: test.sh every script execution home directory for 30 minutes 00: 00-23

0,30 18-23 * * * /bin/sh /home/test.sh

Example 10: Every Saturday night, 11: 00 pm test.sh execute script in your home directory

0 23 * * 6 /bin/sh /home/test.sh

test.sh script every hour to perform home directory: Example 11

* */1 * * * /bin/sh /home/test.sh

Example 12: between 11 pm to 7 am, every one hour test.sh script execution home directory

* 23-7/1 * * * /bin/sh /home/test.sh
Published 24 original articles · won praise 7 · views 6888

Guess you like

Origin blog.csdn.net/weixin_43968988/article/details/104444638