douhun7609 2017-10-16 22:36
浏览 42
已采纳

Cronjob脚本失败,但通过浏览器工作(Zip Archive PHP)

I have a PHP script that generates large zip files, when I execute it via SSH it works, but the same script fails intermittently when executed via cron.

Things I tried:

  • Changing paths inside the script, but since it executes via SSH it shouldn't be the issue
  • Changing path to the PHP executable inside the cron command (I ran "whereis php" and used the 2 available PHP executable locations)
  • Modified file permissions
  • Put time limit as ini_set('max_execution_time', 990000); and via set_time_limit (990000);
  • Half a dozen support tickets with the hosting company, but they can't help

Last thing I'm trying right now, which I don't think will help is manually setting a time limit after which the cronjob will fail by including: /bin/timeout -s 2 990000 but I think it's useless since normally there are no timelimits, unless I'm missing something.

Log file shows that the script fails after I instantiate an object from class ZipArchive and then try to execute the addFile method.

This is my current cron command:

30 4 * * * /bin/timeout -s 2 990000 /usr/bin/php /home/script.php > /tmp/script.log

Appreciate your help.

  • 写回答

2条回答 默认 最新

  • dso0139 2017-11-20 16:27
    关注

    Unfortunately the only solution to make this work is an ugly work, which works great. It's a simple CURL script that executes the file that crontab cannot execute.. Then I put the CURL file in the crontab instead.

    So I'm executing the CURL script via crontab, that then goes and executes the script that cannot be executed via cron. It's ugly, but it works..

    # Open a PHP/CURL session
    $s = curl_init();
    
    # Configure the PHP/CURL command
    curl_setopt($s, CURLOPT_USERAGENT,'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
    curl_setopt($s, CURLOPT_URL, "HTTP://YOUR_DOMAIN.COM/SCRIPT.PHP"); // Define target site
    curl_setopt($s, CURLOPT_RETURNTRANSFER, TRUE);  // Return file contents in a string
    curl_setopt($s, CURLOPT_BINARYTRANSFER, false);  // Indicate binary transfer
    curl_setopt($s, CURLOPT_REFERER, "https://google.ca");     // Referer value
    curl_setopt($s, CURLOPT_SSL_VERIFYPEER, FALSE); // No certificate
    curl_setopt($s, CURLOPT_FOLLOWLOCATION, TRUE);  // Follow redirects
    curl_setopt($s, CURLOPT_MAXREDIRS, 4);          // Limit redirections to four
    
    # Execute the PHP/CURL command (send contents of target web page to string)
    
    if($run_the_script = curl_exec($s))
        echo "cron executed!";
    

    The cron script the the following in the header, it's loose protection against normal users executing the script:

    if (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['SERVER_ADDR'] != $_SERVER['REMOTE_ADDR']) die('Permission denied.');
    

    ^ Check that the file is being executed by a script that resides on the same IP as the server

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题