Skip to content
Advertisement

PHP shell_exec() returning different result than SSH Terminal

I have a Linux machine running CentOS 7 with Python 2.7.5 installed. I am trying to run the Arduino-CLI, which is an executable that requires Python. I wrote a command to compile a sketch. As seen on the right side, it effectively compiles.

When I run the same command in PHP using sudo_exec(), it gives me:

Error during build: exec: "python": executable file not found in $PATH

For context, I am logged in as the web-user on both the terminal and PHP. What could I do to fix it?

Screenshot

Advertisement

Answer

I ended up figuring out this problem. In shell_exec(), PHP does not define the PATH environmental variable. It also won’t know the current working directory. So adding these couple lines before shell_exec() did the trick:

chdir("/home/gyropalm/subdomains/app/studio");  //set current working directory
putenv("PATH=/usr/bin/");   //required to define python path or Arduino-CLI complains
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement