盛世宏图 发表于 2025-2-20 09:46:44

Linux 之 crontab 【配置定时任务】

场景

要求每个小时 定时到/home/threeinf 目录下执行 sh run.sh restart 下令
配置

1. crontab -e 编辑定时任务列表(相当于vi /etc/crontab, 就是编辑crontab文件)
crontab -e 在文件中添加
0 * * * * cd /home/threeinf && sh run.sh restart 保存退出【esc -> :wq ->enter(回车)】
2. crontab -l 查察当前的定时任务列表
crontab -l https://i-blog.csdnimg.cn/blog_migrate/34d39c192fbbc414b5203a39121163c3.png
3 重启定时服务
systemctl restart crond.service
4.到/var/log/cron查察定时任务是否执行
tail -f /var/log/cron https://i-blog.csdnimg.cn/blog_migrate/2f172b6f5c27744794b2bcd53b4c7bee.png
存在输出即可,完结撒花
留意

shell脚本手动执行没题目,crontab定时执行失败

原因:
crontab执行定时任务不会缺省的从用户profile文件中读取环境变量参数(用户登陆Linux操作体系的时间,”/etc/profile”, “~/.bash_profile”等配置文件会被自动执行)
所以常常导致在手工执行某个脚本时是成功的
Shell脚本缺省的 #!/bin/sh 开头换行后的第一行添加

#!/bin/bash
source /etc/profile
source ~/.bash_profile

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Linux 之 crontab 【配置定时任务】