python - 如何使用cron调度运行python程序

标签 python ubuntu cron

我想要一个 python 网页抓取程序每天在某个时间运行。为此,我在 ubuntu 的 cron 中使用此命令

28 22 * * * root /home/ahmed/Desktop python hello.py

这根本行不通。一定有什么问题。有人可以帮我吗?

最佳答案

尝试将 #!/usr/bin/python (称为 shebang 行)添加到 Python 脚本的顶部,然后

28 22 * * * root /home/ahmed/Desktop/hello.py

您必须使脚本像这样可执行(作为单独的命令运行): sudo chmod +x/home/ahmed/Desktop/hello.py

来自Shebang page在维基百科上:

Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.[8] For example, if a script is named with the path "path/to/script", and it starts with the following line: #!/bin/sh then the program loader is instructed to run the program "/bin/sh" instead (usually this is the Bourne shell or a compatible shell), passing "path/to/script" as the first argument.

如果您不想更改任何内容,这也可以:

28 22 * * * root python/home/ahmed/Desktop/hello.py

关于python - 如何使用cron调度运行python程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22387883/

相关文章:

python - 使用从颜色图中获取的颜色绘制直方图

python - 作为工具提示的容器不显示内容

c - 在内联 C 程序集中执行系统调用会导致段错误

shell - cron 作业的容错能力如何?

python - Cron 作业与高级 Python 调度程序

python - 如何将参数传递给 Google App Engine 上的 python cron 任务?

python - 如何将具有所选行平均值的行添加到数据框中

python - 我应该像 C++ 一样优化我的 python 代码吗?有关系吗?

node.js - NodeJS 4 和 5 npm 安装 bcrypt 和 db-migrate 失败

即使是 super 用户,MySQL 访问也被拒绝 - 错误 1045 (28000)