Running complicated ROS shell command through php - php

I'm trying to run a ROS shell program on the server through php on Ubuntu 14.04. I have tried using system, exec, shell_exec but nothing happens and I don't get any output. The system call is the following:
echo shell_exec("/opt/ros/indigo/bin/rosrun gazebo_ros spawn_model -database Part_A -gazebo -model Part_A");
What are the limitations of using system or exec to run any shell command through php on a server?
I don't care as much about the output of the command as for its execution. I think the problem has to do with the fact that PHP doesn't have any PATH like shell does so it can't find any applications without specifying the exact location. How can I make PHP se the same PATH shell does?

The problem was that the apache user and the environment in which the bash commands are running are not set up correctly. I followed the instructions from this answer but instead of using "source" I used "." and instead of using the source.bash file I used the source.sh file. I also set all the environment variables that had to do with ros or gazebo using the putenv() function.

Related

Using bash, how to change the name of a file based on feedback from PHP script?

I have git bash and I am making commits using it. I need however, to make the .bashrc alias, that will also run a php script, return a value N (which is a number) from that script, and use that value Nto change the name of a file like so:
nameOfFileN
How do I do this? I need this sequence of commands. It is my local windows machine. I have php installed. I use windows git bash to use git. I want to run this in it (in the git bash). I need this bash command together with the php file that just returns an echo-ed variable value.
If your PHP script is hosted in a webserver, use curl:
curl http://localhost/path/to/script.php
If it is a stand-alone script and you have the PHP CLI interpreter installed, run the script directly:
php /path/to/script.php
Both calls will give you the output of the PHP script. Use it however you need (pipe it to another process, redirect it to a file, use it for command substitution, assign it to a shell variable, …)

'python' is not recognized as an internal or external command, operable program or batch file using PHP passthru()

I am trying to invoke my python script from php by using passthru() function. I have already done that successfuly and for development I used xampp , now at some moment I installed manually apache,php and other add-ons.
I also made changes to apache conf to make my python scripts work, some of them work when i invoke them directly via ajax,but scripts like this:
<?php
passthru("python C:/Apache24/htdocs/app1/pyscripts/export_zakup.py GARČIN
2>&1",$retval);
echo $retval;
?>
give me result like:
'python' is not recognized as an internal or external command, operable program or batch file. 1
Also when i copy python C:/Apache24/htdocs/app1/pyscripts/export_zakup.py GARČIN to my cmd it works properly.
I have already spent few hours trying to find out where the problem is but unsuccessfuly. Anyone knows where the problem is?
First, locate where the Python executable is by runniing which python from the command line:
$ which python
$ /usr/bin/python
Then use the full path to the executable in your PHP script:
passthru("/usr/bin/python C:/Apache24/htdocs/app1/pyscripts/export_zakup.py GARČIN
2>&1",$retval);
echo $retval;
Keep in mind that the path will be different on Windows vs. Linux based machines. For example the latest versions on Windows typically install the executable in C:\Python27\, so the full path would be C:\Python27\python

PHP shell_exec won't call Windows 10 bash script

I'm running a CakePHP project under XAMPP (Apache) on Windows 10 Anniversary Update.
Apache is running under my user account.
The app calls several external processes via shell_exec(): ImageMagick, phantomjs execute as expected.
I also want to call a bash script, that in turn calls ImageMagick under Ubuntu bash (installed separately, via apt-get). I've had to adjust all paths into a form that bash can resolve.
bash /mnt/e/Projects/project-name/website/bin/crop_to_aspect.sh 1 1
/mnt/e/Projects/project-name/website/webroot/images/agent_photo/tmp_ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
/mnt/e/Projects/project-name/website/webroot/images/agent_photo/ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg```
The command fails when called via shell_exec(). The same command, written to CakePHP's log, and then called from a cmd.exe prompt, works perfectly.
Thinking it may have been a path issue, I wrapped the same script in a windows batch file, including the full path to bash. I called the full batch file path, ie:
#echo off
SET aw=%1
SET ah=%2
SET in=%3
SET out=%4
C:\Windows\System32\bash.exe /mnt/e/Projects/project-path/website/bin/crop_to_aspect.sh %aw% %ah% %in% %out%
This script is then called:
E:\Projects\project-path\website\bin\crop_to_aspect.bat 4 3
/mnt/e/Projects/project-path/website/webroot/images/listing_photo/tmp_ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
/mnt/e/Projects/project-path/website/webroot/images/listing_photo/ed564289-a6f6-45b7-b9f3-2aec2b8bb3d1.jpg
Once again, the command executes correctly in cmd.exe but does nothing when run via shell_exec() from the PHP script.
Check if a function is not in the list of functions disabled in php.ini (disable_functions) line

PHP shell_exec cannot find environment variable

I have added this line PATH DEFAULT=${PATH}:~/bin/ to the ~/.pam_environment
This allows me to call ffmpeg from command line without path which is obviously in ~/bin/ dir so everything works fine as long as im ussing command line.
But if try to run the exact same command from php all i get is sh: ffmpeg: not found
And the code is
shell_exec("ffmpeg 2>&1");
So from my very little experiance with linux (in this case Ubuntu to be specific) i guess apache has no access to pam_environment or ~/bin
What can i do to make this work?
look at the output of phpinfo(), it has a section with all environment variables it sees. then look at your webserver configuration, maybe it's sanitizing the environment, or maybe the init script which starts your webserver does it.
and is the account the webserver is running under using PAM at all?

Using the `where` command through PHP

I am trying to get the path to certain exe's using the where command in the command prompt on windows.
Here is what i did in command prompt.
where g++
where java
where javac
where python
All of these are giving the correct output of the path in the console window which indicated that I have set the environment variables correctly.
But now When i try to run the commands using the shell_exec() function in PHP, only the call to where java and where python gives the correct output. I was even able to successfully execute a respective test file using these commands through PHP.
But strangely, where g++ and where javac give this error in the browser when run through PHP:
INFO: Could not find files for the given pattern(s).
Also if I get the outputs of these two commands on the console and then copy that into my script to compile a c++ or java file, it works perfect. But the where command returns the above INFO when run through the PHP script.
I am running the server on localhost using XAMPP. Any idea what is missing?

Categories