# need help on python cgi script that reboots/shutdowns my box

## Cocoy

Hello, 

I'm writing a python cgi script for my home gentoo server that shutdowns/reboots the machine by simply pressing a button on the local webpage. i know Webmin can do this out of the box... but i need one for non-tech members of the family. 

I hope someone can point to me where i've made a mistake.

My script (please don't mind the mess... i'll clean it up as soon as it works):

```

#!/usr/bin/python

import cgi, syslog, os

syslog.syslog('i am here')

form = cgi.FieldStorage()

syslog.syslog('begin main')

if form.has_key("Restart"):

         syslog.syslog('begin restart')

         print "Content-type: text/html\n"        

         myHTML = "<HTML><HEAD><Title>Warhammer</Title></HEAD><Body> <div align=center

r><h1>...reboot in progress...</h1></div></Body></HTML>"

        print myHTML

        goExec = "sudo -u cocoy /sbin/reboot"

        os.system(goExec)

        syslog.syslog('reboot script activated')

elif form.has_key("Shutdown"):

        syslog.syslog('begin shutdown')

        print "Content-type: text/html\n"        

        myHTML = "<HTML><HEAD><Title>Warhammer</Title></HEAD><Body> <div align=center><h1>...Warhammer has received your command to power off and is shutting down...</h

1></div></Body></HTML>"

        print myHTML

        goExec = "/sbin/shutdown -t1 -h now"

        os.system(goExec)

        syslog.syslog('shutdown in progress')

else: syslog.syslog("there is no key!")

```

* * * 

i've tried even setting sudoers file to run apache, nobody and my username without passwords.

syslog tells me that the script was executed:

Mar 12 17:11:04 warhammer python: i am here

Mar 12 17:11:04 warhammer python: begin main

Mar 12 17:11:04 warhammer python: begin restart

Mar 12 17:11:04 warhammer sudo:   apache : TTY=unknown ; PWD=/var/www/localhost/htdocs/ubiquitous ; USER=cocoy ; COMMAND=/sbin/shutdown -t1 -h now

Mar 12 17:11:04 warhammer python: reboot script activated

Mar 12 17:20:01 warhammer cron[17239]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons )

Mar 12 17:22:18 warhammer python: i am here

Mar 12 17:22:18 warhammer python: begin main

Mar 12 17:22:18 warhammer python: begin shutdown

Mar 12 17:22:18 warhammer python: shutdown in progress

the files have chmod 755. 

the files have chown apache:apache (i've tried apache:root, after setting up root as the group apache runs on)

the box doesn't seem to reboot/shutdown... is it some permission i have to set? do i need to adjust sudoers? do i need to use init? because i tried that and it reflects the same way.... is it something else? 

hope you guys can point me out what am i missing.

thank you.

----------

## Cocoy

[/code]edited my sudoers and made sure that apache can run the script as a privileged user, then edited the above code to this: 

```

goExec = "sudo /sbin/reboot"

```

----------

