I'm trying to launch a julia program from a drupal website (running on xampp for Windows), it needs to run asynchronously in the background while the php script continues execution. This is the code I'm trying to use:
$juliaFile = escapeshellarg(DRUPAL_ROOT . '/sites/all/modules/tsap/Modeling/runme.jl');
$cmd = "start /B julia $juliaFile";
pclose(popen($cmd, 'r'));
This code works perfectly if I run it through a command line php script, but it doesn't work when it runs through apache, however, the next bit of code works both from command line and from the webserver (The only difference is running a php script instead of a julia program)
$phpFile = escapeshellarg(DRUPAL_ROOT . '/sites/all/modules/tsap/Modeling/runme.php');
$cmd = "start /B php $phpFile";
pclose(popen($cmd, 'r'));
I've also tried calling the first block of code within a php file that gets execute by the webserver, which also succeeds from the command line, and fails when the server attempts to execute it.
I also get issues using backtick operators and exec() (they block on the call) and using COM::run() results in the same issue as pclose(popen())
Does anyone have any ideas for getting the julia call to work?
Thanks for your time
add the '&' end of your command, which put the command in the cackground.
You also can use proc_open() to run the command background. You also refer the post of mine that use the proc_open() to run commands background.
Related
This question already has answers here:
How do you run a .bat file from PHP?
(7 answers)
php How do I start an external program running - Having trouble with system and exec
(3 answers)
Closed 3 years ago.
Before everyone starts butting in with "security risks" "cant be done" stop there and read the ENTIRE post
I have a web server set up from a home laptop which is serving as a games web server im trying to create a GUI so its easier for us to maintain the server and im trying to use batch files to do the actions on the computer
So to put this into perspective I have my index file index.php
<form method="post">
<input type="submit" name="startServer" value="Start Server">
</form>
<?
if(isset($_POST['startServer'])){
exec('batch/startServer.bat');
}
?>
And my startServer.bat will run on the laptop running the server and will do all the actions nesscary to start our game server so there is another directory "Instance" containing an excutable "Server.exe" which the batch file will run
The issue im having is running the web server and testing this it doesnt work if I open the batch file directly it works but it seems the php code doesnt work
For clarification I am using apache and my browser is chrome
And just a quick question for anyone willing to answer the route im going is correct right? Using php would allow everything to run on the machine hosting the server so the end user will only see the GUI and the server would run the batch files and everything on the web server and not the local machine if that makes sense?
EDIT: To be more clear about what's going on the function exec runs but it just hangs like the application is loading I need a solution that will actually open the application are my host computer for example if I wanted to open up notepad I press a button on the Web server and notepad will open on the computer
EDIT 2: I would like to note that I dont exactly need to use the exec function and I have tried all the answers to date 7/19/2017:3:45pm none are working if I do something on the sorts echo exec('start text.bat'); I will get a This is a test to show your batch is working and simply just have echo ..... in the batch file the main issue I am having is the server is not physically showing the opened file like displaying the GUI lets just take notepad for example
I can open notepad and get some return value as long as my batch file closes notepad once its finished running however the GUI for notepad is never displayed and thats very important
I read in a few articles about using apache as a service which im pretty sure I am but I do know that xaammp has suffiecient priveleges and I have checked the box that says "Allow apache to interact with desktop" however no GUI is popping up thats the main point I guess im trying to get across is I need to display the GUI not just open the file as a background service.
If it makes answering easier I am open to switching programming languages if theres one that can do what I want easier
Your theory is correct, it will run on the server however you may have issues running applications directly from php (with this method afaik it does not detach from the PHP, and the webapp "hangs" while the application is running).
Make sure: return values are printed / logged. Just an
<?php
if(isset($_POST['startServer'])){
echo exec('batch/startServer.bat');
}
?>
Could point you to the right direction. The exec function may have been disabled in your distribution.
Using
<?php
instead of
<?
is highly advised, by default short_tags are not enabled in most distributions (wamp, xamp, etc).
Set debug mode and print everything to get information about the problem:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST['startServer'])){
echo exec('batch/startServer.bat');
}
?>
If you don't have any response, try a simple batch file with a "hello world" to test if it works.
Be aware, the rights and limitations are comes from the php environment, the batch file inherits the same rights running the PHP code / Apache (in case of mod_php)
In php manual about exec function, there is a note :
Note: If a program is started with this function, in order for it to
continue running in the background, the output of the program must be
redirected to a file or another output stream. Failing to do so will
cause PHP to hang until the execution of the program ends.
I think "hangs like the application is loading" is your application waiting for the bat file terminated / closed to get the output result
Let's try another approach, i found it here, the concept is to create a scheduler that execute the program you want and call it using command.
hope this help :
shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
If this doesn't work
Check whether you have declared safe_mode = Off inside php.ini
From here:
How do you run a .bat file from PHP?
Have you tried:
system("cmd /c C:[path to file]"); ?
You might need to run it via cmd, eg:
system("cmd /c C:[path to file]");
Or Try following options
1.
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat');
?>
2.
When you use the exec() function, it is as though you have a cmd terminal open and are typing commands straight to it.
Use single quotes like this $str = exec('start /B Path\to\batch.bat');
The /B means the bat will be executed in the background so the rest of the php will continue after running that line, as opposed to $str = exec('start /B /C command', $result); where command is executed and then result is stored for later use.
<?php
pclose(popen("start /B test.bat", "r")); die();
?>
i think this is a containment issue.
if you run the app under the process of php run by iiswebuser when php terminates it will close all spawned child processes in windows. there is a very quick way a command to break an application out of the child process containment using the start command.
if(isset($_POST['startServer'])){
exec('start batch/startServer.bat');
}
Diagram of containment as i explained it (simplisticly)
IIS (IIS runs as an IISUser)
php (application)
cmd.exe (batch)
using start bring it to the root of that tree
IIS (IIS runs as an IISUser)
php (application)
cmd.exe (batch)
Baim Wrong was correct in the first part of the response: you have to redirect output of the script or your PHP code will hang. Also, you have to move process in the background.
This is easy to do on *nix:
system("/usr/local/bin/shell.sh >> /tmp/log.log 2>&1 &");
I know that you can redirect the output on Windows but not sure how to move the process in the background. You should check DOS manual or try with power shell.
you can use either system or exec php function
$path = __DIR__ . '/batch/startServer.bat';
exec('cmd /c start ' . $path);
or
$path = __DIR__ . '/batch/startServer.bat';
$lastLine = system('cmd /c start ' . $path);
You are having some issue about running application directly from exec. I was having the same issue of running file using exec. It was solved by passing another parameter 2>&1.
exec('some_command 2>&1', $output);
print_r($output); // to see the response to your command
Check the values printed by output see exec function
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$output = array();
if(isset($_POST['startServer'])){
exec('batch/startServer.bat 2>&1', $output);
print_r($output);
} else {
echo "Not posted";
}
?>
I have the following problem:
I hava a xampp sever running and I want it to execute a powershell. A php triggers a .bat file which contains the following code:
#echo
cd C:\OpenBR\bin
start /WAIT br -algorithm FaceRecognition -compare C:\xampp\htdocs\upload C:\xampp\htdocs\DP C:\xampp\htdocs\results\result.csv
start /WAIT C:\xampp\htdocs\CSVconvert\sortieren.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\Removedouble.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\remove_path.ps1
start /WAIT C:\xampp\htdocs\CSVconvert\remove_foo.ps1
start C:\xampp\htdocs\CSVconvert\remove_quoatation.ps1
The first part works fine, up until the point when i want to exec the powershell "sortieren.ps1". When I run the batch manually, it executes and does the job, when triggered via php, it doesn't.
I set "Set-ExecutionPolicy Unrestricted" in both x86 and x64 shells.
I am just confused because the normal command line works and powershell doesn't, even after setting it on unrestricted.
I viewed
executing a Powershell script from php
and
PowerShell on Windows 7: Set-ExecutionPolicy for regular users
but couldn't solve the problem.
What did i miss?
The session you are running those commands in doesn't have the same environment variables as when you are using PowerShell to run them manually. You'll have to specify the absolute path to the powershell executeable and the scripts that you want to run so that they will be found.
start /WAIT C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\xampp\htdocs\CSVconvert\sortieren.ps1
Since the problem was the environment i thought you might benefit from a package that handles that aspect automatically. Here is a project that allows PHP to obtain and interact dynamically with a real Powershell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell');
$strCmd1 = 'first command from first script';
$return1 = $shellObj->exeCmd($strCmd1);
$strCmd2 = 'second command from first script';
$return2 = $shellObj->exeCmd($strCmd2);
Instead of triggering a single script you can just trigger each command individually and handle the return. You can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script.
I'm new to linux system and I'm trying to make a PHP script to be ran infinite times. Note that I'm using Debian 7.
So, I'm using a screen to open a window, so far so good, I have the worker.php file already running succsefully, and I need to make shell script which runs the php script infinite times.
So I've come up with this:
#!/bin/sh
for (( ; ; ))
do
/usr/bin/php worker.php
sleep 1
done
The problem is , when trying to run ./worker.sh in the screen , I get this error:
bash: ./worker.sh: /bin/sh^M: bad interpreter: No such file or directory
So I've stripped of the for, and replaced it with a simple echo , which results into the same error, so I've wrote this question because I don't know what's wrong, both sh or bash exist on the server, I'm wondering if the shebang is wrong but.. I have the automysqlbackup script which starts with the same shebang.
Do you have any idea what is wrong ? I'm just a newb.. don't really know much.
If you're wondering why am I running a file every second, it's because this file serves as a commands processor from a queue in a game. And running it with cron every minute is too slow. MySQL triggers are not fitting my needs, so I'm forced doing this.
Regards.
From the message it looks like you have a <cr><lf> at the end of your shebang line (the #! one). As isn't a valid line end on debian unix (it is on windows and some other varieties of unix), it is being taken as part of the filename, and so the o/s can't find the program to run.
Fixing it would require something like this:
tr -d '\015' < worker.sh > worker_nocr.fixed
Also, as you're using bash as your shell, you might wish to change the shebang to use bash as well, or other things might not work which work fine when you type them in at the command prompt
I'm trying to setup a php trigger file that will set off a background process. (see this question)
I'm doing this on a Windows Wampserver environment.
So for example I have trigger.php that runs the exec function that calls for my backgroundProcess.php to be parsed and executed.
However the problem is that my trigger.php file is waiting for the exec() command to finish running backgroundProcess.php before it stops. The background process runs for about 20-30 seconds, and trigger.php is waiting all that time until backgroundProcess.php has fully finished.
Is that making sense? Here is the trigger.php file that runs the exec() command
exec('C:\wamp\bin\php\php'.phpversion().'\php.exe -f C:\path\to\backgroundProcess.php > C:\wamp\bin\php\php'.phpversion().'\dev\null &');
Basically, I'm wanting trigger.php to just trigger off the backgroundProcess and not wait around for it to finish.
Problem solved with the following command:
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:\wamp\bin\php\phpVERSIONNUMBER\php-win.exe -f C:/wamp/www/path/to/backgroundProcess.php", 0, false);
Tried to achieve the same on a Windows 2000 server with PHP 5.2.8.
None of the solutions worked for me. PHP kept waiting for the response.
Found the solution to be :
$cmd = "E:\PHP_folder_path\php.exe E:\some_folder_path\backgroundProcess.php";
pclose(popen("start /B ". $cmd, "a")); // mode = "a" since I had some logs to edit
ps :
Posting the same reply to the other thread (PHP on a windows machine; Start process in background) since these 2 links helped me a lot in doing some research on this.
From the manual : http://www.php.net/manual/en/function.exec.php
Note:
If a program is started with this
function, in order for it to continue
running in the background, the output
of the program must be redirected to a
file or another output stream. Failing
to do so will cause PHP to hang until
the execution of the program ends.
And a similar question I answered : Call another PHP script and return control to user before the other script completes
You may need to change your implementation approach. Having to wait for such a long time would be an annoyance for the user of your app and fatal for the entire app.
For such tasks, it's usually better to queue the task, ideally on database, and process them periodically. There are chron jobs on Linux based systems. In Windows, you can use a scheduler to launch the backgroundProcess.php.
In addition to Rohit's answer above, I edited his solution to work on Windows 10 PHP 7.0.3:
pclose(popen("start /B ". $cmd, "w"));
It may well be that when using the exec() in a windows environment that redirection is to NUL:. /dev/null is a *nix null file.
I need to run a Python script in the background after being called from a PHP file. The PHP file should continue to run independently of the Python script (i.e. it shouldn't hang waiting for the Python script to finish processing, but should instead carry on processing itself).
The Python script takes one argument and produces no output (it merely processes some data in the background), then exits. I'm running Python 2.6, PHP 5.2.6, and Ubuntu 9.04.
You could use exec() to kick off the Python interperator and have it send its output to either a file or to /dev/null with redirection. Using the & operator in the exec call will cause the command to be started and PHP to continue without waiting for a result.
http://www.developertutorials.com/tutorials/php/running-background-processes-in-php-349/ goes into more detail.
PHP Process Control can be used for this. The proc_open command can be used to start a process. You can later check up on it, read it's output etc.
View the manual entry: http://www.php.net/manual/en/function.proc-open.php and search around google for PHP Process Control
I'm guessing the PHP file is called via Apache, in which case you won't be able to fork(). You should make your Python script daemonize. Check out python-daemon.
You could use:
<?php
shell_exec('./test.sh &');
?>
where ./test.sh should be the execution line to your script