Search code examples
phpcroncommand-line-interfaceplesksafe-mode

PHP cli command line safe_mode restriction


I'm using the Kohana framework (3.0.9), which generates daily logs. I want to mail the log file if one was made the day before CRON runs the script, but after days trying I can't figure out how to put off safe_mode in PHP CLI modus.

When I'm running my script on the web, there is no problem. But I want to run the script as a CRON task on my Plesk 9.5.2 server (or on the command line as root user) I'm getting the following error:

ErrorException [ 2 ]: dir(): SAFE MODE Restriction in effect. The script whose uid is 10001 is not allowed to access /var/www/vhosts/mydomain.com/subdomains/mysubdomain/httpdocs/application/logs/2011/01 owned by uid 48 ~ APPPATH/classes/controller/ajax.php [ 181 ]

I've allready put SAFE MODE off in my Plesk control panel, which works fine for the web request, but not in on the command line or as an CRON task.

I'm using the following code to test if its working:

$d = dir(APPPATH.'logs/2011/01/');
echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
   echo $entry."\n";
}
$d->close();

I can read the directory APPPATH.'logs/', and also the directory APPPATH.'logs/2011', but the directory's representing each month with the daily log files always give an error.


Solution

  • At nowadays you can schedule php script execution from UI like this:

    plesk schedule cron url php script

    In case you still need execute script via command line pay attention that Plesk's PHP binaries are placed in:

    # 7.0
    /opt/plesk/php/7.0/bin/php
    # 5.6
    /opt/plesk/php/5.6/bin/php
    # 5.5
    /opt/plesk/php/5.5/bin/php
    # and so on
    

    Original answer:

    I know this is a few months old, but for the next person that comes across a problem while using Plesk and cron and PHP, here's the answer.

    While Plesk does run cron as ROOT, it also runs PHP by default with safe mode ON, which means that when you setup a cron in Plesk that needs PHP, it's going to have restrictions that you do not experience from the shell or from the web.

    So what you do is use the CLI /etc/php.ini option override, like so:

    /usr/bin/php -q -d safe_mode=Off /var/www/vhosts/path-to-your-php-file.php