简体   繁体   English

Python 脚本不通过 crontab 执行

[英]Python script doesn't execute via crontab

I read some similar topics but no luck to fix my issue.我阅读了一些类似的主题,但没有运气解决我的问题。

This is my first Python script, it runs no problem when I manually execute it.这是我的第一个 Python 脚本,当我手动执行时它运行没有问题。

But it doesn't work with below crontab.但它不适用于以下 crontab。

0,15,30,45 * * * * /usr/bin/python /usr/lib/python3.5/venv/send_email_attachment.py >/dev/null 2>&1

I also try to below.我也尝试在下面。

*/2 * * * * /usr/bin/python /usr/lib/python3.5/venv/send_email_attachment.py > /tmp/listener.log >/dev/null 2>&1

The listener.log is generated but empty. listener.log 已生成但为空。

Any idea?任何想法? Thanks谢谢

The form of a cronjob is: cronjob 的形式是:

*  *  *  *  * user-name  command to be executed

You forgot to add the user which runs the cronjob.您忘记添加运行 cronjob 的用户。 Supposing you run it using the root user, change the code to:假设您使用 root 用户运行它,请将代码更改为:

*/2 * * * * root /usr/bin/python /usr/lib/python3.5/venv/send_email_attachment.py > /tmp/listener.log >/dev/null 2>&1

Run this command: sudo /etc/init.d/cron restart运行这个命令: sudo /etc/init.d/cron restart

after setting crontab demon has to restart设置 crontab 恶魔后必须重新启动

Reference: Run a python script from with arguments (from argparse in python) from crontab参考: 从 crontab 使用 arguments(来自 python 中的 argparse)运行 python 脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM