Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm executing a python script from a php page like so: exec("python ./test.py");

This script runs fine if I don't open a serial port in it. If I do, however, (and this is the whole point of calling the python script in the first place), the script doesn't execute properly.

If I call a simple python script that prints a statement -

print "This works!"

Then I get the desired output in my php page. But, if I open a serial port, I no longer get the output of "This works!", and the serial data is not getting sent to the receiving device -

import serial
ser = serial.Serial("/dev/ttyACM0",9600)
print "This works!"

Both scripts run fine from the command line.

Is this a php limitation? I have tried other methods of execution such as popen and system, but they didn't work for me either.

share|improve this question
3  
Running from the command line is done with YOUR permissions. Running it under Apache is doine with Apache's permissions. Check that apache can open ttyACM0. –  Marc B Jul 11 '12 at 2:52
    
Change exec() to system() to see the output. –  Roman Newaza Jul 11 '12 at 2:55
    
@MarcB How would I go about doing that? –  penguinrob Jul 11 '12 at 3:02
    
@RomanNewaza tried system() again, it didn't work. –  penguinrob Jul 11 '12 at 3:04
    
run python -c'import sys; print >>sys.stderr, "stderr is visible"' –  J.F. Sebastian Jul 11 '12 at 3:06

1 Answer 1

up vote 2 down vote accepted

Perhaps you aren't getting complete error reporting from your Python execution. Try adding raise Exception('Boo!') as the first line of you Python program to find out if you are or not. If you don't get the exception and a traceback, then your program is probably failing on the serial.Serial line, but you aren't hearing about it.

share|improve this answer
    
I get the exception when I run from command line, but nothing when run from php. Edit: That is with the exception before anything else. –  penguinrob Jul 11 '12 at 3:00
    
If I add a print statement before the exception, I get the output of it. But it still doesn't output the exception –  penguinrob Jul 11 '12 at 3:05
1  
It sounds like you are getting stdout but not stderr. Either fix your process spawning so you do get stderr, or wrap your entire Python program so that error handling goes to stdout. –  Ned Batchelder Jul 11 '12 at 3:06
    
Alright, sent stderr to stdout and am now getting all the error messages. –  penguinrob Jul 11 '12 at 3:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.