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?

link|improve this question
feedback

1 Answer

up vote 1 down vote accepted

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

Anywho:

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');
link|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 Nov 30 '11 at 2:53
1  
Had to be said ;) – thinice Nov 30 '11 at 3:37
feedback

Your Answer

 
or
required, but never shown

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