解决crontab不执行的一般思路
1. 前言
Linux 下利用 crontab 工具 1 进行定时操作时,会遇到不执行的情况。
一般而言,会有各种各样的原因 2, 3, 4:
- 环境变量
- 换行符:cron 中每个条目下面添加一个空行
- crond 服务
- shell 解释器
- 时区
- 百分号%
- 密码过期
- 权限
- 不同平台
- 不同 cron
- crontable 变量
- GUI
但是,有解决问题的一般思路:查看系统日志
和系统邮件
,找到错误原因,而后解决。
2. 步骤
2.1 查看系统日志
终端命令:
tail -50 /var/log/syslog
crontab 运行的关键字: CRON[1409480]: (name) CMD ...
,
2.2 查看系统邮件
Ubuntu 下安装 mail 的终端命令:
sudo apt install mailutils
终端输入 mail
,选择数字序号,查看部分邮件如下:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/keyring/backends/libsecret.py", line 62, in get_password
items = Secret.password_search_sync(
gi.repository.GLib.GError: g-io-error-quark: Cannot autolaunch D-Bus without X11 $DISPLAY (0)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/name/xxx.py", line 72, in <module>
yag_smtp_connection.send(
File "/home/name/.local/lib/python3.10/site-packages/yagmail/sender.py", line 156, in send
self.login()
File "/home/name/.local/lib/python3.10/site-packages/yagmail/sender.py", line 210, in login
self._login(self.credentials)
File "/home/name/.local/lib/python3.10/site-packages/yagmail/sender.py", line 228, in _login
password = self.handle_password(self.user, password)
File "/home/name/.local/lib/python3.10/site-packages/yagmail/sender.py", line 234, in handle_password
return handle_password(user, password)
File "/home/name/.local/lib/python3.10/site-packages/yagmail/password.py", line 11, in handle_password
password = keyring.get_password("yagmail", user)
File "/usr/lib/python3/dist-packages/keyring/core.py", line 55, in get_password
return get_keyring().get_password(service_name, username)
File "/usr/lib/python3/dist-packages/keyring/backends/libsecret.py", line 68, in get_password
raise KeyringLocked('Failed to unlock the item!') from error
keyring.errors.KeyringLocked: Failed to unlock the item!
简而言之,是 yagmail
的 keyring
出了问题,即调用内置的邮箱密码出了问题。
解决方法很简单,把邮箱密码放在命令里面 5。