I’m trying to shutdown an Ubuntu machine from PHP and am running into an issue if I want to delay the shutdown.
The PHP line I’m using is:
exec("sudo shutdown -h +5 &", $output);
Where 5 is however many minutes in the future I want to shutdown.
My problem is that this won’t background and Apache hangs until either the machine is shutdown or someone else cancels the shutdown. shell_exec() has the same result.
Is there another way to do this that will return immediately?
I have a severe opposition to allowing sudo level privilege escalation for your setup…
Toss ‘sudo shutdown -h’ into a script file.
at -f /shutdown/script `date +%H:%M --date "now + 5 minutes"`
Since you’re using PHP it might make more sense to do the time calculation in PHP:
$time = date('H:i', strtotime('+5 minutes')); exec('at -f /shutdown/script $time');
Source : Link , Question Author : William W , Answer Author : thinice