Linux 计划任务管理

CentOS-Logo

实际的生产环境中,总会做一些定期的任务,比如数据备份,我们不可能总等到那个时间去手动执行,这时计划任务就派上用场了。


一次性计划任务

at

  • 一次性计划任务

at [HH:MM] [YYYY-MM-DD]
设置完,按Ctrl + D退出

1.获取系统当前时间

[root@localhost ~]# date
2019年 08月 28日 星期三 00:30:41 CST

2.设置一次性计划任务

[root@localhost ~]# at 00:33 2019-08-28
at> rpm -qa | wc -l > /opt/test.txt
at> <EOT>
job 1 at Wed Aug 28 00:33:00 2019
[root@localhost ~]# atq
1       Wed Aug 28 00:33:00 2019 a root

3.验证结果

[root@localhost ~]# cat /opt/test.txt
1318

atq

  • 查询还未执行的计划任务
[root@localhost ~]# atq
1       Wed Aug 28 00:33:00 2019 a root

atrm

  • 删除还未执行的计划任务
[root@localhost ~]# at 00:35 2019-08-28
at> shutdown now
at> <EOT>
job 2 at Wed Aug 28 00:35:00 2019
[root@localhost ~]# atq
2       Wed Aug 28 00:35:00 2019 a root
[root@localhost ~]# atrm 2
[root@localhost ~]# atq

周期性计划任务

crontab

  • 周期性计划任务

-e:编辑计划任务列表
-l:列表显示计划任务
-r:删除计划任务列表
-u:指定用户

  • 配置文件:
[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
参数 说明
0~59整数
0~23整数
1~31整数
1~12整数
0~7整数,0和7都表示星期日
命令 普通命令、程序脚本
* 任意时间
- 连续的时间范围
, 间隔的不连续时间
/ 指定的间隔频率
[root@localhost ~]# crontab -e
crontab: installing new crontab
[root@localhost ~]# crontab -l
50 1 * * * /usr/bin/cp -p /etc/passwd /root/passwd.bak
30 1 */3 * * /usr/bin/tar zcvf /opt/log.tar.gz /var/log/
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root

猜你喜欢

转载自www.cnblogs.com/llife/p/11421674.html