0

I want to a create a php in my webpage, button press, as an emergency shutdown command to my local server.

I have the following php code that i am using right now. It is working but it is slow. I found the following post in stackoverflow and i was thinking if i can modify my code and use it.

I need on button press to immediately transfer to redirect page and on background to shutdown server.

My code:

<?php
session_start();
unset($_SESSION['valid']);
unset($_SESSION['timeout']);

header("Location: https://www.google.com/");
system('sudo /sbin/shutdown -h now');
?>

The stackoverflow post:

php exec command (or similar) to not wait for result

YvetteLee
  • 1,057
  • 3
  • 13
  • 27
  • 2
    What is your definition of "slow"? How long does it take? Apart from physically pulling the plug this should be one of the quickest ways. – ArSeN Jan 10 '21 at 14:24
  • @ArSeN it is taking 2-3sec. All that time the browser is showing the webpage. It is freezing from 2-3 sec and then redirect. – YvetteLee Jan 10 '21 at 14:44
  • Does this answer your question? [php exec command (or similar) to not wait for result](https://stackoverflow.com/questions/3819398/php-exec-command-or-similar-to-not-wait-for-result) – ArSeN Jan 10 '21 at 20:38

1 Answers1

0

Updated my answer...

Perhaps adding a "&" at the end of your shutdown command might do the trick:

system('sudo /sbin/shutdown -h now &');

It pushes a command to the background on Linux systems and returns control to the shell immediately and continues to run the command in the background, this might also make your system() call be completed immediately.

Note that this does not guarantee the shutdown actually worked (i.e. perhaps an error was thrown on the server, you won't know that). Also as ArSeN pointed out, this does not speed up the shutdown process itself, but will probably have the system() call be completed immediately and thus your script will continue to run (and redirect) while your server is being shut down.

Also as a side note: this is probably not a very secure thing to do ... why would you want the user that is running the webserver, also have control to be able to shutdown you entire server... That means that anyone with access to that script can shutdown the server ... or anyone with access to your webserver for that matter... if they find a security issue leak and are able to inject CLI commands, they'll also be able to shut down your server.

Dennis
  • 371
  • 1
  • 9