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?

1 Answer 1


I have a severe opposition to allowing sudo level privilege escalation for your setup...


Toss 'sudo shutdown -h' into a script file.

Then run at (scheduler)

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');
  • Thanks, I'm only doing this on a personal server for my roommates and I. I also am only allowing the Apache user to perform this one escalated command.
    – William W
    Nov 30, 2011 at 2:53
  • 1
    Had to be said ;)
    – thinice
    Nov 30, 2011 at 3:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .