【问题标题】:PHP Cron Job set but not runningPHP Cron 作业集但未运行
【发布时间】:2016-12-17 07:05:20
【问题描述】:

我已经使用

设置了玉米作业
exec('echo -e "`crontab -l`\n* * * * * http://example.com/cron/sendsms.php" | crontab -');

我已经看到了这个使用

$output = shell_exec('crontab -l');
echo '<Pre>';
echo $output;
//o/p * * * * * http://example.com/cron/sendsms.php

直到一切正常。

如果我运行这个 url(http://example.com/cron/sendsms.php)。然后它可以正常工作。

但是 cron 没有按照设定的时间执行。有什么问题?

【问题讨论】:

  • 你有 shell 访问权限吗?
  • 没有兄弟。我没有访问权限。

标签: php shell url cron crontab


【解决方案1】:

据我所知,您的 crontab 包含以下行:

* * * * * http://example.com/cron/sendsms.php

URL 被作为 shell 命令调用,就像您在终端中输入 http://example.com/cron/sendsms.php 并按下 Enter 一样。如果你想调用远程 PHP 脚本,你应该使用 HTTP 客户端,例如

* * * * * wget -qO /dev/null 'http://example.com/cron/sendsms.php'

上面的命令向 URL 发送一个 HTTP GET 请求并将响应打印到 null 设备(忽略输出的常用方法)。


顺便说一句,将条目附加到 crontab 的方式在很多方面都是不正确的。

shouldn't be using the shell echo command,改用printf

使用$(...) 代替反引号作为命令替换,因为a) 不可能构建嵌套的命令替换,b) $(...) 形式更具可读性。

避免在 PHP 中使用复杂的 shell 结构。如果你需要一个复杂的 shell 结构,你应该为 shell 脚本创建一个单独的文件。

您可以在 proc_open() 的帮助下附加 crontab 条目,而无需使用 shell 管道。

【讨论】:

    猜你喜欢
    • 2015-01-11
    • 2014-11-02
    • 2016-05-27
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多