2013-05-29 15:22
浏览 85


I've got a PHP script that simply e-mails me a test message. If I go into my webserver cPanel I can create a cronjob that runs the script every 10 minutes and it works perfectly.

I manually schedule the cron job in cPanel using the following settings:

10 * * * * php -q /home1/user1/public_html/mail.php

Again the above works fine, but when I try to create the cron job via PHP instead of cPanel it never runs. When I check the cPanel to see if the job was actually created by my php script it DOES show up. All the settings that show up in cPanel are correct, it just doesn't run the script.

This is the PHP code I'm using to create the cron job:

$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'10 * * * * php -q /home1/user1/public_html/mail.php'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');

I imagine it could be a permission issue or something like that? Not really sure why the job works when I create it, but doesn't when PHP creates it.

图片转代码服务由CSDN问答提供 功能建议

我有一个PHP脚本,只是通过电子邮件给我发送测试消息。 如果我进入我的网络服务器cPanel,我可以创建一个每10分钟运行一次脚本的cronjob,它运行正常。


  10 * * * * php -q /home1/user1/public_html/mail.php

以上 工作正常,但当我尝试通过PHP而不是cPanel创建cron作业时,它永远不会运行。 当我检查cPanel以查看作业是否实际上是由我的php脚本创建时,它会显示出来。 cPanel中显示的所有设置都是正确的,它只是不运行脚本。


  $ output = shell_exec('crontab -l'); 
file_put_contents('/ tmp / crontab.txt',$ output.'10 * * * * php -q / home1 / user1  /public_html/mail.php'.PHP_EOL);\ cocho exec('crontab /tmp/crontab.txt');

我想它可能是一个权限 问题或类似的事情? 当我创建它时,不确定为什么这个工作有效,但是当PHP创建它时却没有。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxiandiyo58855 2013-05-29 16:02

    Ended up figuring this out...I'm sure what was wrong with the php code I was using above but it seems that was somehow causing the execution failure.

    I'm now using the code below and the job schedules and actually runs :)

    exec('echo -e "`crontab -l`
    10 * * * * php -q /home1/user1/public_html/mail.php" | crontab -');
    解决 无用
    打赏 举报

相关推荐 更多相似问题