3

This is a reposted question from raspberrypi.stackexchange.com. While I am trying to get something to work on python on the raspberry pi, since it doesn't involve any pi-specific things, it was suggested by someone I post here instead. Original post is here.


I am trying to make a web ui to change the date in the rapsberry pi, but I keep getting a return code of 256.

Currently what I have goes like this:

web page -> submits an ajax request to a python script python checks what type of command (a time/date command in this case) and pieces together a string looking like:

sudo date --set="20130901 20:10"

and stores it in a variable commandString. Then python goes:

os.system(commandString)

and the return value is passed all the way up to the web ui where it is printed out.

I also currently return the commandString value to the web ui too to verify it and it looks okay.

The problem is that every time I test, I keep getting back 256 as the error return code. The date on the raspberry pi of course doesn't change as I manually check it before and after.

However, if I manually go in to python on the raspberry pi and try:

commandString = 'sudo date --set="20130901 20:10"'
os.system(commandString)

It works with no issues. If I try it without sudo then I get a return value of 256 as well, so I thought maybe it was a permissions issue with my original script. I tried this link to check my script's permissions and it seems to be okay? (os.geteuid() is 0)

If it matters, I am using lighttpd and fastcgi to run python from a web ui. My lighttpd config is currently:

fastcgi.server = (
    ".py" => (
    "python-fcgi" => (
    "socket" => "/tmp/fastcgi.python.socket",
    "bin-path" => "/var/www/command.py",
    "check-local" => "disable",
    "max-procs" => 1)
    )
)

Any ideas on what I'm missing?


On the original post, it was also suggested I try something like:

echo <password> | sudo -S date --set="20130829 02:02

While it's probably not a good idea to put in my root password like that, I tried it and got the same result: it works when doing in the terminal/shell and within the python interpreter, but not via the web ui to python.

Community
  • 1
  • 1
mitim
  • 3,169
  • 5
  • 21
  • 25

2 Answers2

0

UPDATE: or maybe just use /usr/bin/sudo /bin/date ... to make sure the commands are found.

Try this:

import subprocess

p = subprocess.Popen('sudo -S date --set ...', shell=True, stdin=subprocess.PIPE)
p.communicate(input='<your password>')

This is a more proper way of launching a subprocess (via the shell), and sending it some input.

If you also need to read the output of the process, then for example:

p = subprocess.Popen('sudo -S date --set ...', shell=True,
                     stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate(input='<your password>')
print "\n".join("out: " + x for x in out.split('\n'))
print "\n".join("err: " + x for x in err.split('\n'))

...or just take the contents of out and/or err and parse them.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • hello, trying the first method (don't really need the output for this part) now yields an error code of 1. From searching, I think this means "Operation not permitted" ? – mitim Oct 03 '13 at 00:21
  • ok. sorry to post another comment right away, but this is really weird. Just for fun, I tried it without sudo (just date --set=...) and changed p.communicate to p.wait. Interestingly I get my return code of 0 and it seems to work. O.o Any ideas on why adding sudo (with and without password) gives a return code of 1, while not having it lets it go through? (Within the script os.geteuid() was always 0). I think I need to take a break, shutdown my pi then pick it back up later verify as this feels very random on why it's working. =_= – mitim Oct 03 '13 at 00:40
  • yup... this seems to work now. using subprocess instead of os.system and not using 'sudo' with my command. – mitim Oct 03 '13 at 08:29
  • actually that `sudo` thing puzzled me as well—how did you end up getting by without it? – Erik Kaplun Oct 03 '13 at 10:58
  • I'm not sure. Originally I thought to use os.system("date --set=...") as it said my script was ran under pi/root. This works in the python shell, but in a script it returns 256. Figuring it was a permission issue (sources say error codes are * by 256), I added "sudo" and so forth. In the end, I used your suggestion of using python's subprocess but without sudo, going: p = subprocess.Popen("date --set=...", shell=True, stdin=subprocess.PIPE); p.wait(); return p.returncode; I didn't change anything config/permission-wise. I hope this is supposed to be how it works and not just working by chance. – mitim Oct 04 '13 at 00:03
0

Check the environment whether sudo and date(or any other command you want to execute) can actually be found on the search path.

You can also use absolute paths to sudo and other commands, e.g. /usr/sbin/sudo

Ber
  • 40,356
  • 16
  • 72
  • 88