1

I have a raspberry with lighttpd and php. Now I want to display webpage content before the raspberry is rebooting when I click on a link. I have create the page below. But when I load the page it isn’t display the content but it is direct rebooting.

 <?php 
   echo "<html>";
   echo "<head>";
   echo "<meta http-equiv=refresh content=\"15; url=rebooted.html\">";
   echo "</head>";
   echo "<body>";
   echo "<p><b>The device is restarting now. Please wait.</b><p>";
   echo "</body>";
   echo "</html>";

   flush();
   system('sudo /sbin/reboot');
 ?>

Anyone any idea how I can solve this?

2

You can't do that. As soon as your Linux system starts a reboot then no new process can be instantiated.

sudo reboot is almost instantaneous.

Also allowing a PHP program running as user=www-data group=www-data is a security exposure if it can run any process with sudo.

|improve this answer|||||
  • Yes I know it is a security exposure . But that is a choice that I have made – Colin Jan 26 at 10:22
  • Actually new processes can easily be instantiated, unless you are doing reboot -f, which you should not normally do. But that isn't the problem anyway. – David G. Jan 26 at 20:12
0

The problem here is that, while flush(); tells it to send, it doesn't actually finish sending instantaneously. If you put a sleep(0.5); after the flush, there is a much better chance of finishing.

A better solution might be to write a program to use in place of sudo reboot that closes its input and output, sleeps half a second, and invokes the sudo reboot. This will allow the web page output to actually finish before the reboot starts. Perhaps something like:

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
int main()
{
    int i;
    for (i=0; i<1024; i++) close(i);
    open("/dev/null", O_RDONLY);
    open("/dev/null", O_WRONLY);
    open("/dev/null", O_WRONLY);
    usleep(500000);
    /* system("/usr/bin/sudo /sbin/reboot"); */
    execl("/usr/bin/sudo", "sudo", "/sbin/reboot", NULL);
    return 0;
}
|improve this answer|||||
0

Wouldn't this do the job ?

sudo shutdown -r -t 5

Reboot after a delay of 5 seconds.

|improve this answer|||||
0

Thank you all for your responses. I solved it with:

 exec("sudo reboot > /dev/null 2>/dev/null &");
 flush();
 exit;  
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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