u936293
u936293

Reputation: 16284

Shutting down computer from a python script in apache

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?

Upvotes: 1

Views: 939

Answers (1)

Adriano Repetti
Adriano Repetti

Reputation: 67148

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

Upvotes: 1

Related Questions