PHP Cron 作业已设置但未设置 运行

PHP Cron Job set but not running

我已经使用

设置玉米作业
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 没有在设定的时间执行。有什么问题?

据我所知,您的 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 请求并将响应打印到空设备(忽略输出的常用方法)。


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

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

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

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

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