I have a shell file that I run by cron:
* * * * * /bin/sh /var/www/test.sh > /dev/null 2>&1
That file contains just this line:
php -q /var/www/test.php
That php file contains this:
<?php
$command = 'cairosvg -f pdf -o /var/www/test.pdf /var/www/test.svg';
system($command);
$command = 'mkdir /var/www/test_dir';
system($command);
file_put_contents('var/www/test_user.log', posix_getpwuid(posix_geteuid())['name']);
if (file_exists('/var/www/test.pdf')) {
file_put_contents('/var/www/test.log', 'success');
} else {
file_put_contents('/var/www/test.log', 'fail');
}
cairosvg
is a CLI library that turns SVG files into PDFs. This PHP code runs if I go through the steps above, but the file isn't created. If I call the PHP file through URL, the file is created.
The only difference I see is that the process user posix_getpwuid(posix_geteuid())['name']
is www-data
when ran by URL and root
when ran by shell.
The command cairosvg -f pdf -o /var/www/test.pdf /var/www/test.svg
works when I run it myself in terminal as root.
The second command, mkdir /var/www/test_dir
is just a reality check to ensure that system()
is enable for CLI or something else extremely simple. That command works by both shell and URL.
This is a Minimal, Complete, and Verifiable example of a problem on more complex system, so "skip the shell step" or similar answers aren't going to be very helpful for me.
I am running PHP 5.6, Ubuntu 14.04, and Apache.
Why is this PHP code failing when I call by shell rather than when I call it by URL?