一.简介
1.作用
计划任务主要是做一些周期性的任务,目前最主要的用途是定期备份数据.
2.分类
一次性调度at Schedule one-time tasks with at. 循环调度执行cron Schedule recurring jobs with cron.
二.一次性调度执行at
1.语法格式
at <TIMESPEC>
now + 5min 5分钟后
teatime tomorrow ( teatime is 16:00) 下午茶时间
noon + 4 days 第四天中午
5pm august 3 2020 2020年8月3日下午5点
4:00 2019- 11- 27 某年某月某日某时
2.示例
[root@localhost ~]
at> useradd uuuu
at> <EOT>
job 1 at Wed Aug 5 16:34:00 2020
[root@localhost ~]
1 Wed Aug 5 16:35:00 2020 a root
[root@localhost ~]
三.循环调度执行cron
1.简介
cron的概念和crontab是不可分割的。 crontab是一个命令,常见于Unix和Linux的操作系统之中。 用于设置周期性被执行的指令。 该命令从标准输入设备读取指令,并将其存放于“/etc/crontab”文件中,以供之后读取和执行。
2.查看进程状态
[root@qianfeng ~]
[root@qianfeng ~]
root 550 0. 0 0. 0 126300 1648 ? Ss 10:05 0:00 / usr/ sbin/ crond - n
systemctl disable crond 关闭开机启动
systemctl enable crond 开启开机启动
systemctl stop crond 关闭cron程序
systemctl start crond 开启cron程序
3.cron示例
3.1管理方式
1.创建计划 “crontab -e” Edit jobs for the current user 2.查询计划 “crontab -l” List the jobs for the current user 管理员可以使用 -u username, 去管理其他用户的计划任务 3.删除计划 “crontab -r” Remove all jobs for the current users.
3.2计划任务存储位置
[root@localhost ~]
3.3语法格式Job format
0 * * * * / mysql_back. sh
* / 5 * * * * / mysql_back. sh
0 2 1, 4, 6 * * / mysql_back. sh
0 2 5- 9 * * / mysql_back. sh
* * * * * / mysql_back. sh
0 2 * * * / mysql_back. sh
0 2 14 * * / mysql_back. sh
0 2 14 2 * / mysql_back. sh
0 2 * * 5 / mysql_back. sh
0 2 2 6 5 / mysql_back. sh
00 02 * * * ls
00 02 1 * * ls
00 02 14 2 * ls
00 02 * * 7 ls
00 02 * 6 5 ls
00 02 14 * 7 ls
00 02 14 2 7 ls
3.4示例
1.目的 每隔一分钟执行创建文件的计划任务 2.示例
[root@localhost ~]
/ usr/ bin/ touch / root/ `date + % F-% H-% M-% S`. txt
[root@localhost ~]
[root@localhost ~]
* * * * * / root/ 1. sh
[root@localhost ~]