Linux 定时任务 命令解释 定时任务格式详解

目录

时间命令

修改时间和日期

定时任务格式

定时任务执行

查看定时任务进程

重启定时任务


时间命令

复制代码
#查看时间
[root@localhost ~]# date
2021年 07月 23日 星期五 14:38:19 CST
---------------------------------------
[root@localhost ~]# date +%F
2021-07-23
----------------------------------------
[root@localhost ~]# date +%T
14:35:47
------------------------------------
[root@localhost tmp]# date +%F\ %T
2022-01-11 10:07:50
#########################################

修改时间和日期

复制代码
#修改时间和日期
[root@localhost ~]# date -s '20200723 14:40:00'
2020年 07月 23日 星期四 14:40:00 CST
# 修改时间
[root@localhost ~]# date -s '14:40:00'
#同步时间,如果时间和当前时间不一致,可以做一下时间同步,来让时间准确起来
systemctl restart chronyd 
# 一次执行完是有延迟的,等待一会才看到准确时间,前提是我们有网

定时任务格式

复制代码
#定时任务的格式
* * * * * cmd
分 时 日 月 周 命令
分:0-59
时:0-23
日:0-31
月:1-12
周:1-7
#每5分钟执行一次
*/5 * * * *
#每1小时的01分执行一次
01 */1 * * *
#每半个小时执行一次,下面的意思是每小时的00分和30分各执行一次
00,30 */1 * * *
#每天晚上8:00执行一次
00 20 * * *
#每个月1号晚上8:00执行
00 20 1 * *
#每年1月1号晚上8:00执行
00 20 1 1 *
#每周1、周三、周五晚上8:00执行一次
00 20 * * 1,3,5
# 几个符号的意思:
# * 每分钟
# */5 每5分钟
# 05 第5分钟
# 每秒钟执行的任务,需要单独写脚本,繁琐一些。

定时任务执行

复制代码
#查看定时任务,遇到特殊符号%,需要添加转义符号\;
[root@localhost ~]# crontab -l
* * * * * echo `date +\%T` >>/tmp/time.txt
#编辑定时任务
[root@localhost ~]# crontab -e
示例:
* * * * * date >> /tmp/time.txt # 每分钟执行一次
[root@localhost ~]# crontab -l
* * * * * date >> /tmp/time.txt
我们可以通过cat来查看任务是否执行了,但是比较麻烦,每次手动输入cat,所以我们可以用如下指令
tail -f /tmp/time.txt #监测文件尾部内容的变化.

查看定时任务进程

复制代码
[root@localhost ~]# tail -f /tmp/time.txt
2023年 03月 24日 星期五 10:58:01 CST
2023年 03月 24日 星期五 10:59:01 CST
2023年 03月 24日 星期五 11:00:01 CST
2023年 03月 24日 星期五 11:01:01 CST
# 是这个进程再帮我们执行定时任务:
[root@localhost ~]# ps -ef|grep cron
root 581 1 0 18:05 ? 00:00:00 /usr/sbin/crond -n

重启定时任务

复制代码
# 我们还可以自行重启这个进程
root@localhost ~]# systemctl restart crond
[root@localhost ~]# ps -ef|grep cron # 可以看到进程启动时间变化了
root 2611 1 25 21:27 ? 00:00:00 /usr/sbin/crond -n
相关推荐
2601_9492210315 小时前
Splashtop赋能企业级远程办公全场景安全连接成选型优选
运维·人工智能·安全
七度黑光15 小时前
用 openclaw 给故障复盘打分:质量审核自动化实践
运维·服务器·前端·数据库·自动化
xuefeiniao16 小时前
docker.desktop无法启动,导出镜像后
服务器·docker
123过去16 小时前
nfc-list使用教程
linux·网络·测试工具·安全
last demo17 小时前
mysql
运维·数据库·mysql·oracle
灰阳阳17 小时前
Dockerfile实践-构建Nginx镜像
运维·nginx·docker·dockerfile
evo-master18 小时前
网络故障排除方法
linux·服务器·网络
Johnstons18 小时前
网络流量监控工具怎么选
运维·网络·网络故障排除·网络流量分析·网络性能监控
爱学习的小囧19 小时前
VMware Horizon 8 智能卡认证信任库配置攻略:新增 Root CA 导入指南
服务器·esxi·vmware·horizon
信创DevOps先锋20 小时前
中国DevOps工具链选型新趋势:合规、协同与生态融合的平衡之道
运维·gitee·devops