从cronjob运行时,PHP无权读取或写入文件

时间:2016-12-10 14:10:00

标签: php linux permissions cron crontab

我有一个PHP脚本,必须每15分钟在服务器上运行一次(Ubuntu 16.04)。它从命令行或浏览器运行得很好,但它不是来自cron。问题是,它没有记录和删除文件。

PHP脚本:

<?php

require ("cijfercheck_pass.php");
include ("postmessage.php");
include 'vendor/autoload.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://rapid.deltion.nl/user/login/');
curl_setopt($ch, CURLOPT_POSTFIELDS,'username='.urlencode($login_email).'&password='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "https://rapid.deltion.nl/student/info/");
$page = curl_exec($ch);

curl_setopt($ch, CURLOPT_URL,'https://rapid.deltion.nl/student/pdfresultaten/id/97041344/Luuk%20Wuijster.pdf');
curl_setopt($ch, CURLOPT_POSTFIELDS,'btnresultatenpdf='.urlencode('pdf resultaten'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/my_cookies.txt");
$result = curl_exec($ch);
curl_close($ch);

$destination = dirname(__FILE__) . '/pdf/temp.pdf';
$file = fopen($destination, "w+");
fputs($file, $result);
fclose($file);

function logdata($body){
    $time = date("d-m-Y H:i:s");
    $file = 'cijfercheck.log';
    $current = file_get_contents($file);
    $current .= "[${time}] ${body} \n";
    file_put_contents($file, $current);
}

if (file_exists('pdf/temp.pdf')) {
    logdata("temp.pdf gedownload van rapid.deltion.nl");
} else {
    logdata("ERROR: kon temp.pdf niet downloaden van rapid.deltion.nl");
    die();
}

$parser = new \Smalot\PdfParser\Parser();
$pdf1 = $parser->parseFile('pdf/cijferlijst.pdf');
$pdf2 = $parser->parseFile('pdf/temp.pdf');

$string1 = $pdf1->getText();
$string2 = $pdf2->getText();

$nospace1 = str_replace(' ', '', $string1);
$nospace2 = str_replace(' ', '', $string2);

$arr1 = explode(':', $nospace1);
$arr2 = explode(':', $nospace2);

$check = $arr1[5];
$temp = $arr2[5];

if (!empty($temp) OR !empty($check)) {
    logdata("pdf omgezet naar een array");
} else {
    logdata("ERROR: was niet mogelijk om pdf om te zetten naar array");
    die();
}

if($check == $temp){

    if(unlink('pdf/temp.pdf')){
        logdata("temp.pdf is verwijderd");
    } else {
        logdata("ERROR: kon temp.pdf niet verwijderen");
        die();
    }

} else {
    if(unlink('pdf/cijferlijst.pdf') && rename('pdf/temp.pdf', 'pdf/cijferlijst.pdf')){
        logdata("cijferlijst.pdf is verwijderd en temp.pdf is hernoemt naar cijferlijst.pdf");
        PostToSlack();
    } else {
        logdata("ERROR: kon cijferlijst.pdf niet verwijderen en/of kon temp.pdf niet hernoemen naar cijferlijst.pdf");
    }

}

crontab条目:

0,15,30,45 * * * * php /var/www/html/slack/cijfercheck/cijfercheck.php

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed`enter code here

看起来您缺少用户名,请尝试:

*/15 * * * * root /usr/bin/php /var/www/html/slack/cijfercheck/cijfercheck.php