I have written a script which contacts another server, downloads (PHP + curl) a list of Zmprov commands and then carries out a PHP exec() to commands to zmprov. After that it pings another file on the remote server to tell the remote server it is complete. This
works absolutely fine when I run it with
Code:
php -f /path/to/fetch_commands.zmp
However when I put it in the zimbra users crontab, for some reason the zmprov part fails with no error.
Here is the script:
Code:
<?php
$sourceUrl = 'https://secure.domain.com/tools/zimbra.zmp';
$dumpPath = '/tmp/zimbra-commands.zmp';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sourceUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
echo "Fetching Commands...\r\n";
$result = curl_exec($ch);
// Only write to file if needed
if(strlen($result) > 0) {
$fp = fopen($dumpPath, 'a');
fwrite($fp, $result);
fclose($fp);
// Now we've written the commands, we need to push them to zmprov
echo "Setting up accounts..";
exec("zmprov < /tmp/zimbra-commands.zmp"); // This is FAILING when ran from a cron
// Delete them as we don't need to do them again.
unlink($dumpPath);
// Now we need to ask the server to clear out the commands
curl_setopt($ch, CURLOPT_URL, 'https://secure.domain.com/tools/zimbra-post-update.php');
curl_exec($ch);
}
curl_close($ch); Here is the cronjob and how I set it up:
Code:
sudo su zimbra
crontab -e
*/10 * * * * php -f /tmp/fetch_commands.php
The PHP script definitely runs, as it contacts my post-update file on my server and it's logged.