Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

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

Your Answer

 
discard

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.