目 录CONTENT

文章目录

linux下使用crontab命令配置定时任务

懿曲折扇情
2022-04-20 / 0 评论 / 3 点赞 / 302 阅读 / 1,310 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-05-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

1.crontab命令操作指导,因为刚开始进入编辑模式,不知道保存退出是啥样的,这里敷个教程。

image-1650460965254

操作命令如下:

crontab -u root -e 打开crontab的配置文件,并编辑完成
Ctrl + O 写入内容, 出现File name to Write …,输入Enter
Ctrl +X 保存并输出,保存完成
配置定时任务时间介绍:
每一分钟执行 */1 * * * *
每五小时执行 0 */5 * * *
每天执行 0 0 * * *
每周执行 0 0 * * 0
每月执行 0 0 1 * *
每年执行 0 0 1 1 *
界面如下:
编写脚本的时候,一定要导入python环境变量, 要不容易不执行,这里我卡好久才最后发现是环境变量的问题,要么就是脚本权限的问题

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
22 20 * * * export PATH=/usr/bin/:$PATH;cd /home/array/src;python3 run.py

2.编辑内容保存之后,查看命令crontab -l

image-1650461011080

3.然后配置完成之后,确定任务是否启动,去查看你执行的任务是否产生log或者标志性东西

比如我的python3 run.py执行之后,report目录下会产生log

4.遇到不执行的情况如下总结

1.一定要查看环境变量是否正确
2.或者脚本是否具有可执行权限
3.然后手动执行看是否成功
4.写一个最简单的每分钟执行的demo,看是否会执行
每一分钟执行 */1 * * * *
5.是否安装cron服务等
6.服务是否启动
目前就遇到这些情况,加油加油 老铁!

3

评论区