Unable to run python script from php

382 views Asked by At

I am using a local web server (LAMP). When running shell_exec/exec/system with the parameter "/script.py passed_arg[1] passed_arg[2]" there is no return value and nothing happens.

I eliminated permissions by chmod 777, and unreadable file (readdir() came back true).

I eliminated Apache configuration problems (php.ini disable_functions do not have shell_exec/exec and no safemode on).

when trying to run a different script that prints hello everything works fine.

I think it's a problem with the script content. I am using a catkin workspace in ROS, but even when copying the script to /var/www/html/ it doesn't work (running the same exact command in the terminal works perfectly fine).

When commenting out all ros commands (specifically 'import rospy') it works (whatever is left of it).

Script content:

#!/usr/bin/env python

## Simple talker demo that published std_msgs/Strings messages
## to the 'chatter' topic

import sys
import rospy
from std_msgs.msg import String

def talker():
    X_Coordinate = float(sys.argv[1])
    Y_Coordinate = float(sys.argv[2])
    Guest_First_Name = sys.argv[3]
    Guest_Last_Name = sys.argv[4]
    Office_Name = sys.argv[5]
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True) 
    rate = rospy.Rate(10) 
    hello_str = "go_to "+ str(X_Coordinate)+" "+str(Y_Coordinate)+"     "+Guest_First_Name+" "+Office_Name
    rospy.loginfo(hello_str) 
    pub.publish(hello_str) 
    rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass
1

There are 1 answers

2
dseifert On

You need to check the Apache logs for any error output or indication of what is going on. Otherwise it's hard to say for sure and it's just guess work.

But based on your updated question, the issue seems to be that you are not providing the ROS environment to the webuser. I.e. /opt/ros/$ROSDISTRO/setup.bash is not being sourced when the webserver runs your Python script. This will make e.g. import rospy fail and hence your script.

You should change your set up so that the python interpreter when called knows where to find rospy. This would mean adding the appropriate path to the PYTHONPATH env variable (do echo $PYTHONPATH after sourcing setup.bash on the terminal to see what should be appended). You may need to set other env variables, e.g. ROS_ROOT or similar (check the error output and then compare printenv | grep ROS).