惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

梓言堂 - Yuk's Blog

使用Obsidian优雅地写Hugo博客文章 魔改PaperMod主题和博客改动 Android 提取boot.img 并安装Magisk Android抓包和中间人攻击实现HTTP数据拦截篡改 从Wordpress迁移到Hexo,我都做了些什么 脚本助手:云手机上跑脚本的好伙伴 Caddy搭建Typecho并用脚本监控重启 Sony Xperia XZ1(G8342)强刷笔记 Android软件数据备份以及管理优化 以更低的价格购买和续费域名 使用AirtestIDE和iOS-Tagent实现iOS端自动化测试 Python虚拟环境大杂烩
记一次使用Crontab的错误排查
Yuk · 2022-04-01 · via 梓言堂 - Yuk's Blog

我们常常会有一些定时任务处理的需求,Linux上大多使用Crontab来实现。

趁机记录下最近新开的一台机上使用Crontab的错误排查。

系统是Ubuntu20.04

前置需求

每天定时运行一次虚拟环境下的Python脚本并留存运行日志保存起来。

最初实现

根据需求,很容易写出下面的规则。

crontab -e
# '>>' 代表追加;2>&1 代表错误输出重定向标准输出,即记录所有输出
15 8 * * * ~/XXX/start.sh >> ~/XXX/run.log 2>&1

start.sh内容如下

source venv/bin/activate
python push_main.py

信誓旦旦的等待中,到点后却发现run.log一直都是空的,出现问题了。

错误排查

  1. 首先检查命令。

    ~/XXX/start.sh >> ~/XXX/run.log 2>&1
    

    没问题,正常输出追加log。

  2. 查看Crontab状态和执行日志。

    # 查看Crontab状态
    service cron status
    ...
    # 查看Crontab执行日志
    grep cron /var/log/syslog
    ...
    

    发现时间和本地时间不同,并没有执行命令,方便调试,这里我们调成和本地一样的东八区。

    timedatectl set-timezone Asia/Shanghai
    # 重启系统日志和Crontab使其修改生效
    systemctl restart rsyslog
    service crond restart
    
  3. 时区正确后查看执行日志发现找不到source命令,且找不到venv/bin/activate

    Crontab所用的/bin/sh不包含source命令,用.代替运行脚本即可。

    找不到venv/bin/activate,是因为Crontab执行默认是在用户的根目录下,需要提前手动cd,在start.sh中添加cd ~/XXX/

  4. 导出的log中文自动转码

    Crontab执行的环境变量和用户不同导致的,在start.sh最顶部中添加export LANG=zh_CN.UTF-8导入即可。

修正后的start.sh

export LANG=zh_CN.UTF-8
cd ~/XXX
. venv/bin/activate
python push_main.py

总结

Crontab执行环境和用户执行环境差的有点多,很零碎的参考,记得快忘的也快。