at
一次性调度执行
at
[root@localhost ~]# mow +5min (五分钟后执行)
[root@localhost ~]# teatime tomorrow(明天下午四点)
[root@localhost ~]# noon +4days (第四天中午)
[root@localhost ~]# spm august 3 2020(2020年8月3日下午5点)
[root@localhost ~]# 4:00 2020-8-27 (某年某月某日某时)
设置一个定时设置用户的任务
[root@localhost ~]# at mow +2min
at> useradd user (创建一个用户名为user的用户)
CTRL+D输入完成并提交任务(两分钟后执行任务)
查询任务
[root@localhost ~]# atq
5 Wed Aug 5 19:23:00 2020 a root
验证结果
[root@localhost ~]# id user
查出用户信息即可
crontab
用于设置周期性被执行的指令,该命令从标准输入设备读取指令,并将存放于/etc/crontab文件中,以供之后读取执行
查看进程状态,crond程序运行是计划任务执行的根本
[root@localhost ~]# sestemctl status crond (启动crond程序)
[root@localhost ~]# ps aux | grep crond(查看crond程序的进程)
管理方式
创建计划
crontab -e
查询计划
crontab -l
删除计划
crontab -r
计划任务存储位置
[root@localhost ~]# ls /var/spool/cron
示例
[root@localhost ~]# * * * * * ls (每分钟执行一次查看)
*分别代表时间
例如
0 * * * * (每小时的0分执行)
*/5 * * * * (每五分钟执行)
- 2 * * * (每天的两点执行)
0 2 1,4,6* * (每月的1.4.6日两点整执行)
0 2 5-9 * *(每月的5-9日两点整执行)
0 2 2 6 5 (6月2日的两点整执行,6月的周五两点整执行)
目的:每分钟执行创建文件的计划任务
步骤
1:准备创建脚本文件
[root@localhost ~]# vim /root/1.sh
/usr/bin/touch /root/date +%F-%H-%M-%S
.txt
chmod +x /root/q.sh
2.编写任务计划
[root@localhost ~]# crontab -e
-
-
-
-
- /root/1.sh
3.查看创建结果
[root@localhost ~]# ls -l /root/
- /root/1.sh
-
-
-