doucuo8618 2013-05-29 15:22
浏览 95
已采纳

使用Crontab运行php脚本

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.

  • 写回答

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 -');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 易优eyoucms关于二级栏目调用的问题
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上
  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境