10.linux 定时任务at与crontab

简介:

1、at一次性任务

1.1 命令at安装

    从文件或标准输入中读取命令并在将来的一个时间执行,只执行一次。at的正常执行需要有守护进程atd.

#安装at
    yum install -y at
#启动守护进程
    service atd start 
#查看是否开机启动
    chkconfig --list|grep atd 
#设置开机启动
    chkconfig --level 235 atd on


1.2 使用

    如果不使用管道|或指定选项-f的话,at的执行将会是交互式的,需要在at的提示符下输入命令:

[root@node1 ~]# at now +2 minutes  #执行at并指定执行时刻为现在时间的后两分钟
at> echo hello world > /root/a.txt #手动输入命令并回车
at> <EOT>                #ctrl+d 结束输入
job 2 at 2017-07-24 16:08         #显示任务号及执行时间

选项-l或命令atq查询任务

[root@node1 ~]# atq
2       2017-07-24 16:21 a root

[root@node1 ~]# at  -l
2       2017-07-24 16:21 a root

到达时间后任务被执行,生成一个新文件file并保存echo的输出内容

[root@node1 ~]# cat a.txt 
hello world


at指定时间的方法:
1)hh:mm小时:分钟(当天,如果时间已过,则在第二天执行)
2)midnight(深夜),noon(中午),teatime(下午茶时间,下午4点),today,tomorrow
3)12小时计时制,时间后加am(上午)或pm(下午)
4)指定具体执行日期mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年)
5)相对计时法now + n units,now是现在时刻,n为数字,units是单位(minutes、hours、days、weeks)


如明天下午2点20分执行创建一个目录

[root@node1]# at 02:20pm tomorrow
at> mkdir /root/temp/x
at> <EOT>
job 11 at Fri Dec 23 14:20:00 2016


选项-d或命令atrm表示删除任务

[root@node1]# at -d 11 #删除11号任务(上例)
[root@node1]# atq
[root@node1]#


可以使用管道|或选项-fat从标准输入或文件中获得任务

[root@node1~]# cat test.txt 
echo hello world > /root/temp/file

[root@node1~] at -f test.txt 5pm +2 days
job 12 at Sat Dec 24 17:00:00 2016

[root@node1~]# cat test.txt|at 16:20 12/23/16
job 13 at Fri Dec 23 16:20:00 2016

atd通过两个文件/etc/at.allow/etc/at.deny来决定系统中哪些用户可以使用at设置定时任务,它首先检查/etc/at.allow,如果文件存在,则只有文件中列出的用户(每行一个用户名),才能使用at;如果不存在,则检查文件/etc/at.deny,不在此文件中的所有用户都可以使用at。如果/etc/at.deny是空文件,则表示系统中所有用户都可以使用at;如果/etc/at.deny文件也不存在,则只有超级用户(root)才能使用at。


2、crontab

系统中每个用户都可以拥有自己的cron table,同atd类似,crond也有两个文件/etc/cron.allow/etc/cron.deny用来限制用户使用cron,规则也和atd的两个文件相同。


/etc/cron.deny 表示不能使用crontab 命令的用户

/etc/cron.allow 表示能使用crontab的用户。

如果两个文件同时存在,那么/etc/cron.allow 优先。

如果两个文件都不存在,那么只有超级用户可以安排作业。

每个用户都会生成一个自己的crontab 文件。这些文件在/var/spool/cron目录下


crontab -u 指定一个用户

crontab -l 列出某个用户的任务计划

crontab -r 删除某个用户的任务

crontab -e 编辑某个用户的任务


对于系统级别的定时任务,这些任务更加重要,大部分linux系统在/etc中包含了一系列与 cron有关的子目录:/etc/cron.{hourly,daily,weekly,monthly},目录中的文件定义了每小时、每天、每周、每月需要运行的脚本,运行这些任务的精确时间在文件/etc/crontab中指定。如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@node1 spool] # cat /etc/crontab
SHELL= /bin/bash
PATH= /sbin : /bin : /usr/sbin : /usr/bin
MAILTO=root
HOME=/
 
# 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
 
# run-parts
01 * * * * root run-parts  /etc/cron .hourly
02 4 * * * root run-parts  /etc/cron .daily
22 4 * * 0 root run-parts  /etc/cron .weekly
42 4 1 * * root run-parts  /etc/cron .monthly
 
 
 
 
第一部分表示分钟(0-59),* 表示每分钟
第二部分表示小时(0-23),* 表示每小时
第三部分表示日(1-31),  * 表示每天
第四部分表示月(1-12),  * 表示每月
第五部分表示周几(0-6,0表示周日),* 表示一周中每天
第六部分表示要执行的任务


anacron的目的并不是完全替代cron,是作为cron的一个补充。anacron的任务定义在文件/etc/anacrontab中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@node1 spool] # cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron
 
# See anacron(8) and anacrontab(5) for details.
 
SHELL= /bin/sh
PATH= /sbin : /bin : /usr/sbin : /usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
 
#period in days   delay in minutes   job-identifier   command
1       5        cron .daily               nice  run-parts  /etc/cron .daily
7       25       cron .weekly              nice  run-parts  /etc/cron .weekly
@monthly 45      cron .monthly             nice  run-parts  /etc/cron .monthly
本文转自   a8757906   51CTO博客,原文链接:http://blog.51cto.com/nxyboy/1950598
相关文章
|
3天前
|
Linux 调度
Linux定时任务调度--crontab与at
Linux定时任务调度--crontab与at
36 0
|
3天前
|
Linux
Linux Crontab 查看定时任务启动没
Linux Crontab 查看定时任务启动没
22 0
Linux Crontab 查看定时任务启动没
|
3天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux定时任务 crontab命令 使用指南
【Shell 命令集合 系统设置 】Linux定时任务 crontab命令 使用指南
45 0
|
3天前
|
关系型数据库 MySQL Linux
【Linux专题_04】Linux安装定时任务Cron
【Linux专题_04】Linux安装定时任务Cron
|
3天前
|
Linux Shell 数据库
Linux如何在一个 Crontab 中安排多个 Cron 作业?
Linux如何在一个 Crontab 中安排多个 Cron 作业?
65 1
|
3天前
|
机器人 Linux 数据安全/隐私保护
Python办公自动化【Windows中定时任务、OS/linux 系统定时任务 、Python 钉钉发送消息、Python 钉钉发送图片】(九)-全面详解(学习总结---从入门到深化)
Python办公自动化【Windows中定时任务、OS/linux 系统定时任务 、Python 钉钉发送消息、Python 钉钉发送图片】(九)-全面详解(学习总结---从入门到深化)
75 0
|
3天前
|
机器人 Linux 数据安全/隐私保护
Python办公自动化【Windows中定时任务、OS/linux 系统定时任务 、Python 钉钉发送消息、Python 钉钉发送图片】(九)-全面详解(学习总结---从入门到深化)(下)
Python办公自动化【Windows中定时任务、OS/linux 系统定时任务 、Python 钉钉发送消息、Python 钉钉发送图片】(九)-全面详解(学习总结---从入门到深化)
67 0
|
Linux
linux 定时任务,每小时执行一次
每小时执行一次为  0  */1  *  *  *  这种写法。 注意的是,很容易粗心写成 *  */1  *  *  * 这种写法,代表每隔一小时每一分钟都执行一次
3440 0
|
3天前
|
安全 网络协议 Linux
linux必学的60个命令
Linux是一个功能强大的操作系统,提供了许多常用的命令行工具,用于管理文件、目录、进程、网络和系统配置等。以下是Linux必学的60个命令的概览,但请注意,这里可能无法列出所有命令的完整语法和选项,仅作为参考
196 2
|
1天前
|
存储 Linux Shell
YUM管理器的命令列表-Linux
YUM管理器的命令列表-Linux
8 0