gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:38:07 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com