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条)

报告相同问题?