我想要一个 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/