如何使用at命令设置python脚本在指定时间执行

发布于 2024-09-24 17:27:11 字数 271 浏览 0 评论 0原文

当我将来尝试使用 cron 来执行我的 python 脚本时,我发现有一个命令,AFAIK,cron 用于定期执行,但我的场景仅在指定时间执行一次。 我的问题是如何将 python 脚本添加到 at 命令中, 还有一些用于控制 at 命令的 python 包

我的开发操作系统是 ubuntu 10.04 lucid,我的产品服务器是 ubuntu-server 10.04 lucid 版本。 事实上,我想通过python脚本将python脚本任务添加到at命令中,该文件的更改可以影响at命令添加或删除新作业

When I try to use cron to execute my python script in a future time, I found there is a command at, AFAIK, the cron is for periodically execute, but what my scenario is only execute for once in specified time.
and my question is how to add python script to at command,
also it there some python package for control the at command

My dev os is ubuntu 10.04 lucid,and my product server is ubuntu-server 10.04 lucid version.
in fact, I want through python script add python script tasks to at command, which file's change can effect at command add or remove new jobs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

策马西风 2024-10-01 17:27:11

这适用于我的 Linux 机器:

echo python myscript | at 10:15

编辑:愚蠢的引用...

This works on my linux box:

echo python myscript | at 10:15

Edit: stupid quoting...

污味仙女 2024-10-01 17:27:11

正如手册页所述,at(例如,与 cron 相反)不尊重 shebang(#!/usr/bin/env python行)。它始终使用 /bin/sh 来运行该文件。

因此,为了运行 python 脚本,您必须

echo python myscript.py | at 10:15

按照 @bstpierre 的建议使用或创建一个附加文件

myscript.sh:

python myscript.py

,这样就

at -f myscript.sh at 10:15

不需要 Shebangs (但也不会造成伤害)。

As the man page says, at (as opposed to cron for example) doesn't respect shebang (the #!/usr/bin/env python line). It always uses /bin/sh to run the file.

So in order to run a python script you have to use either

echo python myscript.py | at 10:15

as suggested by @bstpierre or create an additional file

myscript.sh:

python myscript.py

and then

at -f myscript.sh at 10:15

Shebangs are not necessary this way (but wouldn't hurt either).

猥琐帝 2024-10-01 17:27:11

输入man at,它会解释如何使用它。不同系统的用法会略有不同,因此这里没有必要准确地告诉您。

type man at, it will explain how to use it. Usage will slighty differ from system to system, so there's no use to tell you here exactly.

聊慰 2024-10-01 17:27:11

只需

python FILE | at TIME > app.log

更换:
FILE - 您的 .py 文件(包括 shebang)

TIME - 您的时间

Just do

python FILE | at TIME > app.log

replace:
FILE - Your .py file (include the shebang)

TIME - Your time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文