1

I have a LAMP server on a Raspberry Pi 4 with a web page that is making an AJAX call to a php script that restarts the pi. My script echo's a JSON string response back to the web page indicating it is preparing to restart then it executes the "sudo reboot" command. The script runs 100% of the time and always restarts but the echo is not always fully transmitted prior to the pi rebooting which causes errors.

Example Code:

<?php

  ... other code    

  $tags['status'] = "restarting";
  echo json_encode($tags);

  $cmd = "sudo reboot";
  exec($cmd);

?>

I tried adding a sleep() statement after the echo but that halts the entire script and does not echo until after the sleep. I also tried adding the sleep to the exec command like "sleep 5; sudo reboot" but that does not work either.

If I comment out the reboot statement, I get the echo response 100% of the time. This tells me I have a timing issue. Is there a way to make sure the echo is fully processed prior to issuing the reboot?

I have searched for answers but not finding anything that helps.

ChrisE
  • 53
  • 9
  • If the script only reboots, make the announcement before the ajax call. – Michel Jun 30 '20 at 14:46
  • Mostly a guess, but you could put the delay in the command instead of the PHP code. Something like: `$cmd = "sleep 1 && sudo reboot";` You may also need to redirect output and/or send to the background to prevent `exec()` from waiting. Something like: `$cmd = "sleep 1 && sudo reboot > /dev/null &";` (At that point the `sleep` may no longer be necessary, or perhaps never was. As I said these are mostly guesses that I have no handy way to test at the moment.) – David Jun 30 '20 at 14:46
  • Maybe this helps you https://stackoverflow.com/questions/5415665/show-results-while-script-is-still-executing echo output while script is still running if this works you can put your exec after the sleep function – Baracuda078 Jun 30 '20 at 14:48
  • @David, I tried this but it doesn't help – ChrisE Jun 30 '20 at 14:52
  • @Baracuda078, Thanks for the link but the problem is that the system is restarting which completely kills the script. I need a way to gaurantee that the echo completed before issuing the reboot. – ChrisE Jun 30 '20 at 14:54
  • 1
    `sudo shutdown -r +5` This schould work on a pi to shutdown the machine after 5 min maybe this also works with reboot. https://www.raspberrypi.org/forums/viewtopic.php?t=159745 – Baracuda078 Jun 30 '20 at 14:59
  • @Baracuda078 this does work but I really don't want to wait for 5 minutes or even 1 minute. The time parameter for this command is in minutes. If it was seconds, this would be a perfect solution. – ChrisE Jun 30 '20 at 15:06
  • @Michel it does perform other tasks that each sends status. These all work but the last one is the bear since it kills the entire machine just after echoing the last status. – ChrisE Jun 30 '20 at 15:08
  • https://raspi.tv/2012/how-to-safely-shutdown-or-reboot-your-raspberry-pi with -h you can give a time to shutdown/ reboot. Now you can create that time with php and put it in your exec string – Baracuda078 Jun 30 '20 at 15:14
  • 1
    I once had something similar and decided to use 2 ajax calls: 1 perform the task an send the shutdown message back. 2 when message received, display and wait a few seconds, then send request to reboot. Prevented the race condition. – Michel Jun 30 '20 at 16:06
  • @Michel this is a good idea. Did you find that it was a reliable solution? I always like to have positive feedback from each process guaranteeing that they actually did what they were supposed to. This last step leaves you to assume that the reboot script actually ran. I know with my script, there is no way to know 100% for certain that the reboot took place only that the script reached that point. – ChrisE Jun 30 '20 at 20:40
  • 1
    @Michel I decided to try your suggestion of waiting a short period of time then just sending the reboot request and not worry about the response. This works great. I did add a for loop in the reboot script that loops 1000 times and each time I run a usleep(1000) to sleep for 1ms. I am seeing the response most of the time. So either way the script is working. Thanks for yours and everyone else's suggestions! – ChrisE Jul 01 '20 at 03:41
  • @ChrisE Sorry for the late response. I never had any problem with it. And it has run for 1,5 year now. – Michel Jul 01 '20 at 13:49
  • @Michel thanks for the response. Would you please write your suggestion as an answer so I can mark as answered? Also, please upvote my question. – ChrisE Jul 02 '20 at 14:43

1 Answers1

1

I once had something similar and decided to use 2 ajax calls:

  1. perform the task an send the shutdown message back.
  2. when message received, display and wait a few seconds, then send request to reboot.

This prevented the race condition.

Michel
  • 4,076
  • 4
  • 34
  • 52