运行定时任务

发布于 2024-10-08 19:30:45 字数 504 浏览 5 评论 0原文

在过去的几个小时里,我一直对 cronjobs 感到困惑。我已阅读文档和示例。我了解基础知识和概念,但还没有任何工作。所以我希望能得到一些帮助来解决这个完全菜鸟的困境。

最终目标是安排每天执行 django 函数。在此之前,我想知道我可以安排任何旧脚本运行,首先运行一次,然后定期运行。

所以我想: 1)编写一个简单的脚本(可能是 bash 脚本),它可以让我确定它确实运行成功,或者失败。 2) 安排这个脚本在整点运行

我尝试编写一个 bash 脚本,简单地将一些文本输出到终端:

#!/bin/bash
echo "The script ran"

然后我将其放入 .txt 文件中,

MAILTO = *****.******@gmail.com
05 * * * * /home/vadmin/development/test.sh

但什么也没发生。我确信我做错了很多事情。我从哪里开始解决所有这些问题?

've been puzzling over cronjobs for the last few hours. I've read documentation and examples. I understand the basics and concepts, but haven't gotten anything to work. So I would appreciate some help with this total noob dilemma.

The ultimate goal is to schedule the execution of a django function every day. Before I get that far, I want to know that I can schedule any old script to run, first once, then on a regular basis.

So I want to:
1) Write a simple script (perhaps a bash script) that will allow me to determine that yes, it did indeed run successfully, or that it failed.
2) schedule this script to run at the top of the hour

I tried writing a bash script that simple output some text to the terminal:

#!/bin/bash
echo "The script ran"

Then I dropped this into a .txt file

MAILTO = *****.******@gmail.com
05 * * * * /home/vadmin/development/test.sh

But nothing happened. I'm sure I did many things wrong. Where do I start to fix all of this?

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

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

发布评论

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

评论(1

救星 2024-10-15 19:30:45

Crontab 本身不会发送电子邮件,定义 MAILTO 变量也不会改变它的想法。您需要执行以下操作:

首先,确保 mail -s ADDRESS 实际上是从您的邮箱发送电子邮件。
然后调用crontab -e编辑crontab文件。如下所示:

SHELL=/bin/bash
[email protected]
BASH_ENV=/home/dude/.bash_profile
05 * * * Mon-Fri echo "Hello from Cron script" | mail -s "My Script Output" "${MAILTO}"

保存文件并关闭编辑器,cron 应该会拾取更改。这应该有效,您应该会收到电子邮件。然后,您可以将 echo "Hello from Cron script" 替换为您的脚本。

希望有帮助。祝你好运!

Crontab does not send emails by itself, and defining MAILTO variable doesn't change its mind. You need to do something like this:

First, make sure that mail -s ADDRESS is actually of sending email from your box.
Then invoke crontab -e to edit crontab file. Put something like this:

SHELL=/bin/bash
[email protected]
BASH_ENV=/home/dude/.bash_profile
05 * * * Mon-Fri echo "Hello from Cron script" | mail -s "My Script Output" "${MAILTO}"

Save the file and close editor, cron should pick up changes. This should work and you should get email. Then you can replace echo "Hello from Cron script" with your script.

Hope it helps. Good luck!

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