美文网首页
Linux计划任务

Linux计划任务

作者: iDevOps | 来源:发表于2019-10-08 13:21 被阅读0次
计划任务

两种方式实现计划任务,crontab和at

  • at
    可以仅执行一次就结束
# Centos7
systemctl status atd   # 查看atd服务状态
systemctl start atd    # 开启atd服务
systemctl is-enabled atd # 查看是否开启开机启动
#Centos6
chkconfig --list  |  grep atd
# 创建计划任务
[root@centos-7 /]# date
2019年 10月 06日 星期日 20:26:31 CST
[root@centos-7 /]# at 20:28  # 如果是上午时间加上am,比如7:20am
at> mkdir /home/a.txt   # 要执行的任务
at> <EOT>   # ctrl + d结束
job 2 at Sun Oct  6 20:28:00 2019

# 计划任务的其他写法
at 10:00 2019-10-6  # 2019年10月6日上午10点执行
at now+10min   # 10分钟后执行
at 14:00 tomorrow   # 明天下午14点执行
at 11:00 pm + 3 days   # 3天以后的下午11点执行
at 11:00 < a.txt   # 11点执行a.txt的内容
# 查看计划任务 
# 任务编号: 2
# 执行时间: Sun Oct  6 20:28:00 2019
# 队列: a
# 执行者: root
root@centos-7 /]# at -l
2   Sun Oct  6 20:28:00 2019 a root
[root@centos-7 /]# atq
2   Sun Oct  6 20:28:00 2019 a root

# 查看2号计划任务具体内容
root@centos-7 /]# at -c 2
或
[root@centos-7 ~]# ls /var/spool/at/
a00005018f5d62  spool
[root@centos-7 ~]# tail -5 /var/spool/at/a00005018f5d62
# 删除计划任务
atrm 5  # 删除5号任务
  • crontab
    crond命令定期检查是否有要执行的工作,如果有就执行
systemctl status crond  # 状态
systemctl start crond  # 启动crond服务
systemctl is-enabled crond  # 是否开机启动
# 参数
-u  # 指定用户
-l   # 列出cron服务
-r   # 删除cron服务
-e  # 编辑cron服务
eg:
crontab -u root -l
crontab -u sn -r   # 删除sn用户的cron计划任务

语法


crontab语法
符号 描述 示例
* 代表取值范围内的数字 任意
/ 指定时间的间隔频率 */10 或 0-20/2
- 代表从某个数字到某个数字 2-18
, 多个数字 2,4-8,11

示例:

# 每天2点10分
1 2 * * * 计划任务

注: 用户所有的计划任务,都会在/var/spool/cron/下产生对应的文件

系统级别的计划任务

# 系统任务调度的配置文件
# /etc/crontab

SHELL=/bin/bash   # 执行操作系统使用哪个shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin  # 系统执行命令的搜索路径
MAILTO=root  # 将执行任务的信息通过邮件发送给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
# /etc/cron*
cron.d/       #是系统自动定期需要做的任务,但是又不是按小时,按天,按星期,按月来执行的,那么就放在这个目录下面。
cron.deny     #控制用户是否能做计划任务的文件;
cron.monthly/  #每月执行的脚本;
cron.weekly/   #每周执行的脚本;
cron.daily/     #每天执行的脚本;
cron.hourly/   #每小时执行的脚本;
crontab       #主配置文件 也可添加任务

常见写法

#每天晚上21:00 重启apache
0 21 * * * /etc/init.d/httpd  restart
#每月1、10、22日的4 : 45重启apache。
45 4 1,10,22 * *  /etc/init.d/httpd  restart
#每月1到10日的4 : 45重启apache。
45 4 1-10 * *   /etc/init.d/httpd  restart
#每隔两天的上午8点到11点的第3和第15分钟重启apach
3,15 8-11 */2 * *  /etc/init.d/httpd  restart
#晚上11点到早上7点之间,每隔一小时重启apach
0 23-7/1 * * * /etc/init.d/apach restart
#周一到周五每天晚上 21:15 寄一封信给 root@panda:
15 21 * * 1-5  mail -s "hi" root@panda < /etc/fstab

crontab不支持秒,怎么每两秒执行一次呢?
在脚本的死循环中,添加命令 sleep 2 ,执行30次自动退出,然后添加,计划任务:
* * * * * /back.sh

需求
每天2:00备份/etc/目录到/tmp/backup下面
将备份命令写入一个脚本中
每天备份文件名要求格式: 2017-08-19_etc.tar.gz
在执行计划任务时,不要输出任务信息
存放备份内容的目录要求只保留三天的数据


相关文章

  • 20.Linux中的计划任务

    Linux中的计划任务At单次执行计划任务cron 计划任务的使用计划任务:在某个时段自动执行某个任务。 Linu...

  • 开启计划任务

    Linux 开启计划任务 开启计划任务(指定某个文件在什么时间段启动运行) 1.开启计划任务: service c...

  • linux shell中"2>&1"含义

    linux shell中"2>&1"含义 在计划任务中经常可以看到。例如我们公司的计划任务举例: */2 * * ...

  • 计划任务

    Linux计划任务 1)说明概 念:约定时间执行指定任务举例1:通过计划任务定时发布文章举例2:通过计划任...

  • laravel使用scheduler实现计划任务

    传统的计划任务是使用linux的crontab,每次对计划任务进行修改都要上服务器处理,不方便且不安全。larav...

  • 使用Linux crontab命令自动重启服务

    循环运行的计划任务,Linux系统则是由cron/crond这个系统服务来控制的。Linux系统上面原本就有非常多...

  • Linux计划任务

    1、说明 概 念:约定时间执行指定任务举例1:通过计划任务定时发布文章举例2:通过计划任务凌晨3点将测试服务器项...

  • Linux计划任务

    1、crontab命令选项 # crontab -u <-l, -r, -e> 2、cron文件写法 3、特殊符号...

  • Linux 计划任务

    Linux 计划任务 crontab 执行Python脚本(以Postgresql为例,自动下架过期商品): Py...

  • linux|计划任务

    计划任务分类 at 一次性计划任务 查看任务 crontab 周期性任务 crontab:如何使用crontab ...

网友评论

      本文标题:Linux计划任务

      本文链接:https://www.haomeiwen.com/subject/eaxppctx.html