system() and exec() on php hangs - php

I use php (7.3.7) to run an octave script on a web server (iis on windows 10).
In particular the octave script solves a set of differential equations and produces a text file.
The php script is the following:
<?php
system('cd C:\inetpub\wwwroot\BRwebapp\ && "C:\Octave\Octave-5.1.0.0\octave.vbs" --no-gui --persist compart.m');
?>
Strangely enough, the command:
C:\inetpub\wwwroot\BRwebapp\ && "C:\Octave\Octave-5.1.0.0\octave.vbs" --no-gui --persist compart.m
runs fine when executed on cmd and produces the desired output (.txt file).
The php script, however, hangs. The browser (I have tried both Chrome and Firefox) is just loading with no results (not displaying any errors in the console). I have also used exec() instead on system(), with no results. I have verified that php works (by using <?php phpinfo(); ?>).
Any ideas?
Thanks a lot!
UPDATE I have finally solved this issue by following the instructions
presented here by Marle1:
Foo.cmd won't output lines in process (on website)

Related

How to use PHP to execute AutoHotKey script on Windows Server 2016?

I have a Windows Server 2016 VPS with Plesk and PHP 7.1x.
I am trying to execute a simple AutoHotKey script from PHP using the following command:
<?php shell_exec('start /B "C:\Program Files\AutoHotkey\AutoHotkey.exe" C:\inetpub\vhosts\mydomain.com\App_Data\myahkscript.ahk'); ?>
This is the only line on the page. I have tried different ahk scripts, the current one simply creates a MsgBox.
When I execute my php page, on VPS Task Manager I see three processes created with the expected USR: cmd.exe, conhost.exe and php-cgi.exe. However, my PHP page just sits waiting on the server and nothing actually happens on the server.
I have also tried the same line except replacing shell_exec with exec. This seems to make no difference. I have tried without start /b with both commands. In that case the PHP page completes but no new processes are started.
I cannot find any errors in any logs: Mod_Security, Plesk Firewall, IIS.
Any ideas?
EDIT:
I tried my command from the VPS command prompt and immediately slapped in the face with the obvious issue of the space in 'Program Files'. I quoted the string as shown above and the command works. This eliminated the hang when running from PHP. However, the command still does nothing when executed from the web page.
EDIT:
Based on suggestions from the referenced post 'debugging exec()':
var_dump: string(0)""
$output: Array()
$return_val: 1
One point was that I would probably not be able to invoke GUI applications. That puts a damper on the idea.

PHP exec() under Apache returns null and 255 when running a script

Ok, after having bashed my head on this for hours I decided to ask for help. I have a Windows Server 2008 running Apache 2.4 and PHP 7.1. My application must run a PHP script on the server when the user clicks a button on the browser.
This is working fine on my desktop with Windows 10. However, on the server, exec() returns "null" and an exit code of 255.
I read all I could find on exec() issues and tried the following:
exec("C:\\PHP7\\php.exe -v", $output);
I got the proper response containing PHP's version information.
Then I decided to check the configuration files:
exec("C:\\PHP7\\php.exe --ini", $output);
All files were in place.
Then I decided to perform a syntax check on my script:
exec("C:\\PHP7\\php.exe -d display_errors=1 -l C:\\Apache24\\htdocs\\script.php", $output);
No errors were found.
Finally I decided to check the user account:
exec("whoami", $output);
Got "NT Authority\SYSTEM" as expected. To make sure that the script was able to run under the SYSTEM account I used SysInternals psexec:
psexec -s C:\PHP7\php.exe C:\Apache24\htdocs\script.php
Everything ran smoothly.
In other words, the script shows no problems when executed from the command line, either under a user account or the system account. I have also proven that PHP is being properly invoked by exec().
So, then I decided to check for "hidden" errors in my code adding the following two lines to the very beginning of the script:
error_reporting(E_ALL);
ini_set('display_errors', 1);
But, no joy. And I'm out of ideas.
Can any good soul help me on this?
Thanks a bunch,
Miguel.
Finally! The key to the answer was here: PHP exec() git fetch failing with return value 255.
I was unable to see any errors, even adding the "2>&1" pipe redirection to my commands. After reading that article, I learned that proc_open() is way better than exec(). Quoting from PHP's documentation:
proc_open() is similar to popen() but provides a much greater degree of control over the program execution
So, I replaced my exec() for a few lines of code (refer to the example in the manual) and found that the problem was being caused by the Zend Opcache, which was enabled for CLI. The quickier solution for me was to disable it in the command line:
php.exe -d opcache.enable_cli=0 myscript.php
VoilĂ ! Problem solved!

php exec for cronjob won't start the file (BUT manually) - PHP Version-issue?

For a cronjob I use php's exec. For sure, sometimes include is better (or at least more simple), but not if you start a few things at the same time - and from time to time that's necessary.
Anyway, on the server from my webhoster I have PHP 5.3 - and everyhting works fine! But not on the server for a client, which is using PHP 5.2.7. I don't know if this version is the problem, but I think it's better to mention that.
I have (e.g.) two files in the same directory: cronjob.php and test.php. The last one executes its content by starting the file via webbrowser. But not, if I try to execute it by the different file.
cronjob.php:
<?php
exec('php test.php > /dev/null &');
?>
The webhoster of my client (not the one I use) told me to add the full path to the php and to add php45, otherwise it would execute "in php 4". So I changed it to:
exec('/usr/local/bin/php54 absolutepath2file/test.php > /dev/null &');
...but again, it doesn't start the test.php.
I know, if you start a cronjob directly by server and not as a url it won't execute every runtime environment, but the script is loaded directly with url. AND that's exactly the same result if you open the file via your webbrowser.
The strange thing is, that the webhoster told me by mail, that the cronjob.php starts correctly the test.php... but it won't execute. And to repeat: test.php will work perfectly if you start it manually.
Could be there a problem with safe_mode (<- php 5.2.7)? I also add error_reporting(E_ALL); to get alle possible errors, but there aren't any issues, says the server error log.
Anybody? :-)

PHP Run bash script from php file acting strange

I've written a bash script that launches a browser on an xServer and takes a screenshot of the browsers.
If I run it with the apache2 user (www-data) its working flawless, even when i use the php interactive shell and run it via shell_exec or exec its working perfectly.
However when I run it from my php file via browser it does not seem to work properly.
The script does not seems to run the xterm command (for launching the browser) and takes no screenshots, it only executes sleep and kill commands. I spent the whole day looking for a solution or at least a proper way to debug but i cant seem to fid anything
Turn on errors to start with
error_reporting(E_ALL);
ini_set('display_errors', '1');
Then change your command to
shell_exec('bash /home/daemon/daemon.sh');
The last part of your command pipes all output (including errors) to /dev/null - E.g to no where so you will not see any errors at all.
Do this and re-run the command and you should see errors.

Problem with exec in PHP

I am running through a weird problem with exec's in PHP. When I load in the browser the script x.php, the browser hangs and when I run a ps I can see multiple threads being created. The only way to stop them is by restarting apache.
However, instead of running a php script, if I do something like system('ls'), it works fine. So it seems to be problem when a PHP script tries to run another script using exec/system/passthru (I've tried them all).
x.php is defined as following:
<?php
var_dump(system('php -f t.php'));
?>
t.php is defined as following:
<?php
echo 'Hello world';
?>
Take a look at first comment in exec() manual.

Categories