Webapp for using shutdown on a Pi

After getting my Raspberry Pi Zero W I decided I wanted a way to shutdown or reboot it without having to ssh into it.

First, I installed Apache2 and enabled cgi-bin:

sudo apt-get install apache2

sudo a2enmod cgi

I am unfamiliar with nano, so I needed to change my default editor to vim:

sudo update-alternatives --set editor /usr/bin/vim.tiny

With the new editor I was able to use the command visudo to edit the /etc/sudoers file. I added this line:

www-data ALL = NOPASSWD: /sbin/shutdown

After a reboot, this line allows the default web server identity to use the shutdown command without needing a password.

This means a brilliant fellow can possibly create a URL into my system to shut it down, but a.) the Raspberries are not public facing, and b.) I am not using Raspberries for mission critical applications. If it gets weird too many times, I can always undo it. Right now I find the convenience to be worth the risk.

I chose to use perl for the cgi-bin scripts. Shutdown.pl looks like this:

#!/usr/bin/perl

print "Content-type: text/plain



";

system("sudo /sbin/shutdown -h");

print "System is halting, you can close this window now.";



Reboot.pl looks like this:

#!/usr/bin/perl

print "Content-type: text/plain



";

system("sudo /sbin/shutdown -r");

print "System is rebooting. Close this window to avoid a continual reboot.";



Put these files in the cgi-bin directory, usually /usr/lib/cgi-bin and make them executable by whichever web identity your server is using.

Finally, create an html document that has links to those scripts and you are set. Maybe something like this:

<html> <head> <title>System Maintenance</title> </head> <body style="padding: 10px; background-color: cornsilk;"> <p /> <h1 style="text-align: center; color: darkblue;">Reboot or Shutdown the Berry</h1> <p /> <h2>Click a link below to reboot or shutdown the system.</h2> <p /> <table> <tr> <td> <a href="/cgi-bin/reboot.pl">Click here to reboot.</a> </td> <td> </td> <td> <a href="/cgi-bin/shutdown.pl">Click here to shutdown.</a> </td> </tr> </table> </body> </html>

NOTE: Close the browser window after rebooting the Pi. The browser will continue to try to load the page as the Pi is rebooting. Once the web server hears the request, the system will reboot again.