PHP shell_exec won't call Windows 10 bash script - php

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

Related

PHP shell_exec() Behaves Differently Than Terminal Command Line MacOS

Scanline is a simple command line utility used for scanning documents from a twain scanner. http://blog.scottkleper.com/scanline-command-line-scanner-for-mac/
I am trying to use Scanline through a PHP script using shell_exec(); the same way that I would use it directly from the terminal in MacOS.
When I run Scanline directly from the command line, it detects all the attached scanners and prints them out ./scanline -list
When I run Scanline using shell_exec(), it does not detect any devices.
So far, I have changed the apache user to my local user, and have added the local user to the sudoers file. If I run 'whoami' in shell_exec() it is the same result as if I run it in command line.
I have printed the environment using printenv in command line, and have setup all of the same variables in my php script before executing shell_exec() using putenv(); If I run shell_exec('printenv 2>&1'), it is the exact same environment as when I run printenv in command line.
All the permissions are correct and allow access, and scanline runs when executed through shell_exec() without error (I checked apache's error logs, as well as put a error_reporting(E_ALL); in the top of the PHP file to printout any issues along the way). The only difference in how the program is executed is that in command line the devices are detected, and run through shell_exec(), no devices are found.
Any ideas as to what else I could be missing between command line and using shell_exec() ?
I also tried using system(), exec(), and shell_exec() interchangeably with the same result.

Running complicated ROS shell command through 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.

exec() function working in cron but not manually

I'm controlling my home automation with a Raspberry Pi and webserver. I use this line to turn on my lights (sending an RC signal to wireless power socket).
exec("sudo ./../../../home/pi/wiringPi/examples/lights/action 63 A on");
This is working when the script is ran by a cron job, but when I manually want to execute this command (using a php form and buttons), it does not work. I tried adding $output, $return); and checking $return, and that confirms the exec() function is not executed. However, when I use something like exec("whoami");, the script is executed.
What is it about my command that makes it work only in cron jobs? I had this working once, don't know what happened. Manually sending the command via ssh in the terminal is working normally.
Fix permissions on this command and run it without sudo (for ubuntu default php user is www-data, for suse wwwrun, ...).
Check the path also. I recommend absolute paths.

Execute a bash file located on a different VPS

I have a php webpage located on Webserver1, which is from Host1.
I also have a bash script located in Gameserver1 which is from Host2.
Is there any way to send a command from Webserver1 to Gameserver1 to execute the bash file? The webpage and file are on different VPSs. Both are running Debian 7.
The script is literally one line, to execute a java command via a screen, so the server can start if a player notices it's down. The command's available already so it doesn't need to be a secure way of hiding what the command is.
There are 2 ways I can think of. either create a bash file in Webserver1 that connects through ssh and executes the bash script you need on Gameserver1. then run it through php with exec() command.
Or you can create a php file in Gameserver1 that uses exec() to execute the bash script you need on Gameserver1 and call it using file_get_contents() on Webserver1, which is not that secure since anyone can call that file and run your script.

cURL not working in Windows Task Scheduler

I've a PHP script that uses cURL to perform certain tasks. At the moment, I have the script running every 10 minutes. This is what I'm running via Windows Task Scheduler.
C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\autoscripts\index.php
However, for some reason, whenever the argument quoted above is run through the command line, I get the error "Fatal error: Call to undefined function curl_init()". The script works perfectly when I access it via the browser. Is there any reason why PHP isn't able to access the cURL extension via the command line?
Most likely running from command line does not use any ini file that loads the extensions. Open phpinfo() from the browser, copy path to loaded ini file and change your task to:
C:\wamp\bin\php\php5.4.3\php.exe -c "C:\path\to\php.ini" -f C:\wamp\www\autoscripts\index.php
Figured it out. Basically, on WampServer, there are TWO php.ini files that you need to be aware of.
C:\wamp\bin\php\php5.4.3\php.ini
C:\wamp\bin\apache\apache2.2.22\bin\php.ini
Forgot that the command line uses a different ini file than the web server. :(

Categories