Bit of a hard one to debug as I'm working with both a new version of PHP and a new OS on a new server.
I have a cron management system in PHP that allows me too add / remove or enable / disable cronjobs.
On another current Deb 8 server with PHP 7.2 it works flawlessly using the following function to create a job...
public static function createCronjob($job)
{
exec('echo -e "`crontab -l`\n'. $job .'" | crontab -', $output);
return $output;
}
Where $job
would be something like :
10 0 * * * wget -O - https://website.com/cxs?job=jobTitle >/dev/null 2>&1
And I can also list the contents of the crontab with this, which also spits out two separate arrays, one for active jobs and one for inactive ones...
public static function getCronjobs()
{
exec('crontab -l', $data);
$active = [];
$inactive = [];
foreach ($data as $j) {
if (!empty($j)) {
if (substr($j,0,1) == '#') {
array_push($inactive, $j);
} else {
array_push($active, $j);
}
}
}
$arr = [
'active' => $active,
'inactive' => $inactive
];
return $arr;
}
But I've just set up a new Debian 10 server with PHP 7.4 and none of this seems to do anything anymore? It doesn't add jobs and crontab -l
always returns an empty array (which I assume is because there are no jobs to be found).
I've checked that exec()
is working and also tried adding the www-data
user to /etc/cron.allow
as suggested by a few articles I found, although I never had to do that on the Deb 8 server, but I'm getting no joy at all.
Were there new security measures or code changes introduced in Deb 10 or PHP 7.4 that I'm unaware of that would be preventing this from working or is there something obvious I'm missing here?
-- Edit to include the solution kindly provided by @summea below --
I'm sure others are going to stumble upon this so here is the answer :
a) PHP 7.4 (and 7.3 it seems) require the full path to echo
in exec()
public static function createCronjob($job)
{
exec('/usr/bin/echo -e "`crontab -l`\n'. $job .'" | crontab -', $output);
return $output;
}
But strangely the full path to crontab
is NOT required (but probably good practice) as this still works :
exec('crontab -l', $data);
b) Creating /etc/cron.allow
was a mistake for me. It was refusing access to the crontab
to every other user that wasn't declared in this file. After I deleted it and restarted the cron service /etc/init.d/cron restart
everything works as it should.
Really hopes this saves somebody else some time as it's a bugger!