Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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');
share|improve this answer
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 '11 at 2:53
Had to be said ;) – thinice Nov 30 '11 at 3:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.