1

I have a headless Raspberry Pi which I want to have an easy means for my child to power down. I tried the following script in the apache web server:

import os
sss = os.popen('echo password | sudo -S shutdown -hP now')
print sss.read()

But nothing happens.

Then I tried:

from subprocess import Popen, PIPE, STDOUT
p = Popen('echo password | sudo -S shutdown -hP now', shell=True, stdOUT=PIPE, stderr=STDOUT)
print p.stdout.read()

Also, nothing was output and no work appears to have been done.

How can I do a shutdown from a web page?

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Old Geezer
  • 14,854
  • 31
  • 111
  • 198

1 Answers1

1

For security reasons Apache user cannot run sudo command.

Usually this is an almost mandatory rule to save you from attacks but for a Raspberry PI installation you may not have this problem so you can just add Apache user to sudoers (or, better, uso visudo to edit that file).

Something else? Yes you may simply add shutdown permissions to Apache user, follow this tutorial. In short you have to change /etc/sudoers with:

%groupName ALL= NOPASSWD: /sbin/shutdown
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • Thanks. How do I find out the account running Apache? – Old Geezer Nov 19 '13 at 10:39
  • 1
    @OldGeezer you can call [getuser](http://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python) from phyton but probably it's running on default account (_www-data_ or _apache_) – Adriano Repetti Nov 19 '13 at 10:42