php 一个 crontab 文件中的多个 Cron 作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13277497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Cron jobs in one crontab file
提问by ashutosh
I wanted to implement two cronjobs with different execution time. One cron job is for sending emails and second cron job for validating my application subscriptions.
我想实现两个具有不同执行时间的 cronjob。一个 cron 作业用于发送电子邮件,第二个 cron 作业用于验证我的应用程序订阅。
I write one crontab file and write to two cronjob as follows:
我写了一个 crontab 文件并写入了两个 cronjob,如下所示:
2 * * * * path to mailCronjob mail.php
20 * * * * path to check my application's subscriptions sub.php
The problem is first cronjob is working fine. Mail will delivers fine, but the second cronjob is not working. I tried to run second job manually, its also working fine.
问题是第一个 cronjob 工作正常。邮件将正常发送,但第二个 cronjob 不起作用。我尝试手动运行第二个作业,它也工作正常。
I am using command to set cronjob as:
我正在使用命令将 cronjob 设置为:
crontab crontab_file
when I give command crontab -lit also shows both cronjob in command line.
当我发出命令时,crontab -l它还会在命令行中显示两个 cronjob。
I wanted to ask, am I missing something here, or what should I do to run those cronjobs.
我想问一下,我在这里遗漏了什么,或者我应该怎么做才能运行这些 cronjobs。
回答by paulsm4
FACT: you can run as many cron jobs from a single crontab file as you wish.
事实:您可以根据需要从单个 crontab 文件运行任意数量的 cron 作业。
FACT: you can also run differentjobs as differentusers, each with their own crontab file.
事实:您还可以以不同的用户身份运行不同的作业,每个作业都有自己的 crontab 文件。
SUGGESTION:
建议:
1) Just debug what's wrong with your second job.
1) 调试你的第二份工作有什么问题。
2) It could be path, it could be permissions; it's more than likely environment (the environment for "cron" can be different from the environment for the same user from a command line).
2)可以是路径,也可以是权限;它很可能是环境(“cron”的环境可能与命令行中同一用户的环境不同)。
PS:
PS:
Try this, too:
也试试这个:
回答by Erik Nedwidek
Check the owning user's email and see if an error report has been sent to it.
检查拥有用户的电子邮件并查看是否已向其发送错误报告。
If you need to be a certain user and have that user's environment change your call to
如果您需要成为某个用户并让该用户的环境将您的呼叫更改为
su - -c "/path/to/sub.php" SubScriptUser
If your script only works from a certain directory use
如果您的脚本仅适用于某个目录,请使用
cd /path/to/ && ./sub.php
回答by Luis H Cabrejo
I recall the same problem. For some reason, I had to press enterafter my second cron job. Even in the first cron job, if it is the only job, needs a CR/LF (enter). Cursor needs to be on second line (if there is one cron job)... cursor needs to be on the third line, if there is two cron jobs. I guess, needs to read the file completely to interpret the cron job command. I just share this, because if you dont do that, only the first job will be executed, and skips the second one totally unless enter is pressed at the end of the second job. Best regards and cheers... Test that and let us know.
我记得同样的问题。出于某种原因,我不得不在我的第二个 cron 工作后按Enter键。即使在第一个 cron 作业中,如果它是唯一的作业,也需要 CR/LF ( enter)。光标需要在第二行(如果有一个 cron 作业)...光标需要在第三行,如果有两个 cron 作业。我想,需要完全读取文件来解释 cron 作业命令。我只是分享这个,因为如果你不这样做,只有第一个作业将被执行,并且完全跳过第二个作业,除非在第二个作业结束时按下 Enter。最好的问候和欢呼......测试一下,让我们知道。
回答by Vengarioth
I never did 2 actuall cronjobs in one cron-tab file, but rather had the one cronjob execute every 15 minutes and query the database or look into a config file what tasks are there to execute, maybe this concept helps you.
我从来没有在一个 cron-tab 文件中做过 2 个实际的 cronjob,而是让一个 cronjob 每 15 分钟执行一次并查询数据库或查看配置文件要执行的任务,也许这个概念对你有帮助。

