fix(scheduler): use Unraid plugin cron mechanism instead of /etc/cron.d/varaverk

dcron uses the filename in /etc/cron.d/ as the username, so a file named
'varaverk' ran jobs as a non-existent user and was silently ignored.

Switch to writing /boot/config/plugins/varaverk/varaverk.cron and calling
update_cron (which merges all plugin .cron files into /etc/cron.d/root).

In dev mode (no varaverk.plg registered), fall back to injecting our entries
directly into the live root crontab via crontab -c /etc/cron.d -.
This commit is contained in:
Gmer4Lfe
2026-05-24 18:11:50 -04:00
parent 57d55c647c
commit 03e72b6a06
@@ -1,13 +1,12 @@
<?php
// Scheduler — manages schedule.json and /etc/cron.d/varaverk.
// Scheduler — manages schedule.json and the Unraid plugin cron file.
// schedule.json is per-host, never synced.
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/confform.php';
define('SCHEDULE_FILE', '/boot/config/plugins/varaverk/schedule.json');
define('CRON_FILE', '/etc/cron.d/varaverk');
define('CRON_USER', 'root');
define('CRON_FILE', '/boot/config/plugins/varaverk/varaverk.cron');
function vv_schedule_load(): array {
if (!file_exists(SCHEDULE_FILE)) return [];
@@ -65,10 +64,22 @@ function vv_cron_rebuild(array $schedule): bool {
}
$lines[] = "";
// dcron (dillon's cron, Unraid's system crond) reads /etc/cron.d/ but refuses
// world-writable files and does not use a username field in system crontab format.
// Write to the plugin cron file; update_cron merges all plugin *.cron files into /etc/cron.d/root.
if (file_put_contents(CRON_FILE, implode("\n", $lines)) === false) return false;
chmod(CRON_FILE, 0600);
exec('/usr/local/sbin/update_cron');
// update_cron only picks up .plg-registered plugins. In dev (no varaverk.plg registered),
// inject our entries directly into the live root crontab so they take effect immediately.
if (!file_exists('/var/log/plugins/varaverk.plg')) {
$current = shell_exec('crontab -c /etc/cron.d -l 2>/dev/null') ?: '';
$filtered = preg_replace('/^[^\n]*run_job\.sh[^\n]*\n?/m', '', $current);
$ours = file_get_contents(CRON_FILE);
$combined = rtrim($filtered) . "\n\n" . $ours . "\n";
$tmp = tempnam('/tmp', 'varaverk_cron_');
file_put_contents($tmp, $combined);
exec('crontab -c /etc/cron.d - < ' . escapeshellarg($tmp));
unlink($tmp);
}
return true;
}