linux--Cron

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_38880380/article/details/99625503

1 介绍

Cron是Linux系统中最有用的工具之一,cron作业是在指定时间到来时被调度执行的作业。Cron本身是一个守护进程,在后台运行,通过配置文件“crontab”来根据时间调度指定的作业执行。

1.1 cron,crontab以及anacron的关系

  • cron是大多数linux发行版都自带的守护进程(daemon)
  • crontab(cron table的简称)既可以指cron用来定期执行特定任务所需要的列表文件,又可以指用来创建、删除、查看当前用户(或者指定用户)的crontab文件的命令。
  • anacron不是守护进程,可以看做是cron守护进程的某种补充程序,anacron是独立的linux程序,被cron守护进程或者其他开机脚本启动运行,可以每天、每周、每个月周期性地执行一项任务(最小单位为天)。适合于可能经常会关机的机器,当机器重新开机anacron程序启动之后,anacron会检查anacron任务是否在合适的周期执行了,如果未执行则在anacron设定好的延迟时间之后只执行一次任务,而不管任务错过了几次周期。

1.2 crontab配置文件

  • 系统默认crontab文件为/etc/crontab,以及/etc/cron.d/目录下的文件,有些程序会把自己的crontab文件放在/etc/cron.d/目录下。cron守护进程会检查/etc/crontab以及/etc/cron.d/目录下的文件,根据这些文件中的cron任务所设置的执行时间决定是否执行任务,如果当前时间与cron任务所设置的执行时间相同,则执行任务。
  • 每个用户自己的crontab文件都会被放在 /var/spool/cron目录下,默认为空,可以使用crontab命令创建。cron守护进程会检查/var/spool/cron目录下的文件,根据这些文件中的cron任务所设置的执行时间决定是否执行任务,如果当前时间与cron任务所设置的执行时间相同,则执行任务。

1.3 注意事项

cron执行的任务会在设定好的时刻执行,当机器处于关机状态下并错过了任务执行的时间,cron任务就无法预期执行了。

1.4 安装

Linux发行版在默认情况下都预安装了cron工具。若未预装cron,安装

apt-get install cron

2 Cron配置类型

2.1 系统级Crontab

这些cron作业被系统服务和关键作业所使用,且需要root级的权限才能执行。可以在/etc/crontab文件中查看系统级的cron作业
在这里插入图片描述

2.2 用户级Crontab

用户级的cron作业是针对每个用户单独分开的。因此每个用户都可以使用crontab命令创建自己的cron作业,还可以使用以下命令编辑或查看自己的cron作业。

crontab –e
* * * * * command to be executed
- - - - - -
| | | | | |
| | | | | --- 预执行的命令
| | | | ----- 表示星期0~7(其中星期天可以用0或7表示)
| | | ------- 表示月份1~12
| | --------- 表示日期1~31
| ----------- 表示小时1~23(0表示0点)
------------- 表示分钟1~59 每分钟用*或者 */1表示

在这里插入图片描述

3 操作

3.1 查看状态

service cron status

输出:cron start/running, process 1004

3.2 开启服务

service cron start

3.3 help

man cron
crontab -help

3.4 创建并编辑当前用户的crontab

crontab -e

3.5 列出当前用户的crontab

crontab -l

3.6 删除用户的crontab

crontab -u 用户 -i -r

3.7 crontab文件语法详解及示例

当用 crontab -e 编辑当前用户的crontab文件时,首先写入以下内容

# crontab -e
SHELL=/bin/bash
[email protected]
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# 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

该文件的前三行代码设置了默认环境。cron守护进程并不提供任何环境。SHELL变量设置当cron任务(命令以及脚本)运行时的shell,MAILTO变量设置cron任务执行结果发送的邮箱,PATH设置去哪些目录下寻找cron任务的命令。注释部分则解释一条cron任务的构成,一条cron任务就是一行,要设置多少条cron任务则写多少行。一条cron任务由七个部分组成,从左到右依次为:

  • 分钟(0-59)
  • 小时(0-23)
  • 天(1-31)
  • 月 (1-12):或者可以使用月份的英文单词的前三个字母,比如jan,feb,mar,apr…
  • 星期(0-6):星期天用0或者7都可以,或者可以使用星期的英文单词的前三个字母,比如sun,mon,tue,wed,thu,fri,sat
  • 用户名称(可以省略)
  • 要执行的命令或者脚本目录
    前五个部分的编写注意特殊符号的含义:
  • 如果你想匹配取值范围内的所有值,使用“*”
  • 想匹配某些特殊的值,使用“,”,比如2,4,7就匹配的是2,4以及7。
  • 两个值被“-”连接表示范围,此时匹配的是范围内所有值,包含“-”两边的值,比如4-7匹配的就是从4到7。
  • 想要表达每隔一段时间执行一次任务,使用 “/”, 比如分钟部分中的 “*/10”表示每10分钟运行一次,比如小时部分中的“10-22/2”则表示在早上10点到晚上10点这段时间内,每隔两个小时运行一次。 注意 :当“/”左边的值可以除尽“/”右边的值时,任务才会运行。
  • 示例
每隔5秒执行一次
*/5 * * * * 

每隔1分钟执行一次
0 */1 * * * 

每天凌晨执行
0 0 * * * root command

每周星期天早上五点执行
0 5 * * sun root command

每个月的前10天晚上10点开始每隔10分钟执行一次命令
*/10 22 1-10 * * root command

从星期一到五,每个小时的第10分钟、第20分钟以及第30分钟都执行一次命令
10,20,30 * * * 1-5 root command

从早上10点到晚上10点,每个偶数的小时(比如10点,12点)里每五分钟运行一次命令
*/5 10-22/2 * * * root command

@hourly 代表 0 * * * * ,每个小时运行一次
@daily 代表 0 0 * * * ,每天凌晨运行一次
@weekly 代表 0 0 * * 0 ,每周星期天凌晨运行一次
@monthly 代表 0 0 1 * * ,每个月第一天凌晨运行一次
@yearly 代表 0 0 1 1 * ,每年的头一分钟运行一次
@reboot 重启后执行一次

每天凌晨运行一次
 @daily command

3.8 cron.hourly、daily、weekly、monthly

/etc 文件夹下,有cron.hourly、cron.daily、cron.weekly、cron.monthly文件,里面的脚本,cron服务会每一小时、每一天、每一星期、每一月运行一次。

4 示例

4.1 在指定时间调度Cron job作业

  • cronTest.sh
#! /bin/sh
echo hello >> /home/ubuntu/workspace/hello.txt
  • crontab -e
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

*/1 * * * * /home/ubuntu/crontest.sh
  • 3分钟后生效
    每分钟往hello.txt输入一次hello,用cat打印可以看

4.2 删除log

如4.1脚本中加入
rm log文件

4.3 清除cache

如4.1脚本中加入
echo 1 > /proc/sys/vm/drop_caches
需要系统级别权限

4.4 备用

  • 脚本
#! /bin/sh

# 注释

cd ~/workspace.log
echo "" > trace.log
echo 1 > /proc/sys/vm/drop_caches 
  • copy 到 /etc/cron.hourly/下
    注:由于/ etc / crontab文件使用run-parts,因此filename非常严格,不能有点,脚本中不能有~
run-parts runs a number of scripts or programs found in a single directory 
directory. Filenames should consist entirely of upper and lower case letters,
digits, underscores, and hyphens. Subdirectories of directory and files with
other names will be silently ignored. Scripts must follow the
#!/bin/interpretername convention in order to be executed. They will not
automatically be executed by /bin/sh. The files found will be run in the
lexical sort order of the filenames.

5 不执行的原因

  • cron服务未启动
  • 脚本权限问题
  • 路径问题
    脚本的路径(完整)
    待操作文件路径(完整)
  • 时差问题
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
service crond restart
  • 变量问题

参考

1、Cron–archlinux
2、在Ubuntu 14.04使用cron实现作业自动化
3、ubuntu下使用crontab定时器
4、详解:(cron , crontab , anacron)
5、Cron Job定期删除Log(日志)文件
6、linux定时任务cron配置
7、ubuntu – 无法让cron.hourly工作
8、crontab定时任务不执行的原因

猜你喜欢

转载自blog.csdn.net/qq_38880380/article/details/99625503
今日推荐