I'm trying to compile an executable via PHP with msbuild which compiles my C# source, the majority of the script relies on the executable being created so it must wait for msbuild to compile the source.
If I don't put any sort of while loop it will compile fine and the executable is created but the problem is the rest of the script executes to fast and the end result isn't correct.
so at the moment I'm using this..
exec('C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe C:\Users\Administrator\AppData\Roaming\Compile\Myprogram\Myprogram.sln /p:Configuration=Release');
while (!file_exists('C:\Users\Administrator\AppData\Roaming\Compile\Myprogram\bin\Release\Myprogram.exe')) sleep(1);
However in this scenario it's almost as if the exec command never gets ran at all. It gets stuck in an infinite loop and eventually times out resulting in the exe never being compiled.
Any suggestions on the proper way to go about this?
Try running it as follows:
$output = array();
$cmd = 'C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe C:\Users\Administrator\AppData\Roaming\Compile\Myprogram\Myprogram.sln /p:Configuration=Release && exit';
exec($cmd, $output);
Related
I have called a python script within my PHP page using the code below:
echo shell_exec("python object_detection_tutorial_cmsc265.py");
The python script generates image files in a loop until a certain condition is met. Example is the code below:
while (True):
cv2.imwrite('image' + str(loop) + '.png',image);
if (not condition):
break
When the HTML/PHP page is exited in the browser, I can see that the images are still increasing in number, that means the python is still executing. How will I stop the python script upon exiting PHP page?
Assuming your Python script is running in background, like this, you cannot.
If you want to handle this you will have to handle pid.
What following is kind of dirty, well using any kind of shell_exec is dirty anyway, but keep this in mind.
What you could do:
Make the Python script echo its pid at script startup
Use exec PHP function over shell_exec to get only script's first output line
Kill this pid before leaving your PHP script
Python
import os
print(os.getpid())
# your script
PHP
// code
$pid = intval(exec('python foo.py'))
exec("kill $pid")
Well, I'm not PHP developer, but google just told me it may be better to use posix_kill over exec("kill $pid").
Again, this is not clean code, it's hacky and depends a lot of the system.
This is really important as I could not find anything I am looking for in Google.
How do I know when the application (or is it more appropriate to call it a task?) executed by a command line is done? How does the PHP know if the task of copying several files are done if I do like this:
exec("cp -R /test/ /var/test/test");
Does the PHP script continue to go to next code even while the command is still running in background to make copies? Or does PHP script wait until the copy is finished? And how does a command line application notify the script when it's done (if it does)? There must be some kind of interaction going on.
php's exec returns a string so yes. Your webpage will freeze until the command is done.
For example this simple code
<?PHP
echo exec("sleep 5; echo HI;");
?>
When executed it will appear as the page is loading for 5 seconds, then it will display:
HI;
How does the PHP know if the task of copying several files are done if I do like this?
Php does not know, it simply just run the command and does not care if it worked or not but returns the string produced from this command. Thats why it better to use PHP's copy command because it returns TRUE/FALSE upon statistics. Or create a bash/sh script that will return 0/FALSE or 1/TRUE to determine if command was successful if you are going this route. Then you can PHP as such:
<?PHP
$answer = exec("yourScript folder folder2");
if ($answer=="1") {
//Plan A Worked
} else {
//Plan A FAILED try PlanB
}
?>
It waits until the exec call returns, whatever it returns.
However it might be that the exit call returns although the command it has started has not yet finished. That might be the case if you detach from the control, for example by explicitly specifying a "&" at the end of the command.
My project calls for 3 php scripts that are run with if-else conditions. The first script is loaded on the index page of the site to check if a condition is set, and if it is, it calls for the second script. The second script check to see if other conditions are set and it finally calls for the last script if everything is good.
Now I could do this by just including the scripts in the if statement, but since the final result is a resource hogging MySQL dump, i need it to be run independently of the original trigger page.
Also those scripts should continue doing their things once triggered, regardless of the user actions on the index page.
One last thing: it should be able to run on win and nix.
How would you do this?
Does the following code make any sense?
if ($blah != $blah-size){
shell_exec ('php first-script.php > /dev/null 2>/dev/null &');
}
//If the size matches, die
else {
}
Thanks a million in advance.
UPDATE: just in case someone else is going through the same deal.
There seem to be a bug in php when running scripts as cgi but command line in Apache works with all the versions I've tested.
See the bug https://bugs.php.net/bug.php?id=11430
so instead i call the script like this:
exec("php-cli mybigfile.php > /dev/null 2>/dev/null &");
Or you could call it as shell. It works on nix systems but my local windows is hopeless so if anyone run it on windows and it works, please update this.
I would not do this by shell exec because you'd have no control over how many of these resource-hogging processes would be running at any one time. Thus, a user could go click-click-click-click and essentially halt your machine.
Instead, I'd build a work queue. Instead of running the dump directly, the script would submit a record to some sort of FIFO queue (could be a database table or a text file in a dir somewhere) and then immediately return. Next you'd have a cron script that runs at regular intervals and checks the queue to see if there's any work to do. If so, it picks the oldest thing, and runs it. This way, you're assured that you're only ever running one dump at a time.
The easiest way I can think is that you can do
exec("screen -d -m php long-running-script.php");
and then it will return immediately and run in the background. screen will allow you to connect to it and see what's happening.
You can also do what you're doing with 'nohup php long-running-script.php', or by writing a simple C app that does daemonize() and then execs your script.
I have created a small php script locally that runs a java application in command line. This java application continuously runs and never finishes. As it runs, it outputs command line text. Here is the code:
<?php
set_time_limit(0);
$command = "java -Xms124M -Xmx124M -jar myapp.jar";
$end = " 2>&1";
$in = $command . $end;
$out = exec($in);
var_dump($out);
?>
My problem is that the output is never printed because the app never stops running. Is there a way to get the php to print out each line that is returned as the app is running?
Hopefully I am making sense here (Let me know if I am not).
You might want to take a look at the passthru function and the popen function. These should return output as it occurs (although passthru might buffer the output).
One solution would be to launch your process as a background process and redirect output to a file. You could then read the output file in PHP, but you really shouldn't leave PHP running like that, especially since your java process is expected to never end. A better solution would be to use AJAX polling to have PHP return any recent updates to the output file every few seconds or something.
I have a PHP website and I would like to execute a very long Python script in background (300 MB memory and 100 seconds). The process communication is done via database: when the Python script finishes its job, it updates a field in database and then the website renders some graphics, based on the results of the Python script.
I can execute "manually" the Python script from bash (any current directory) and it works. I would like to integrate it in PHP and I tried the function shell_exec:
shell_exec("python /full/path/to/my/script") but it's not working (I don't see any output)
Do you have any ideas or suggestions? It worths to mention that the python script is a wrapper over other polyglot tools (Java mixed with C++).
Thanks!
shell_exec returns a string, if you run it alone it won't produce any output, so you can write:
$output = shell_exec(...);
print $output;
First off set_time_limit(0); will make your script run for ever so timeout shouldn't be an issue. Second any *exec call in PHP does NOT use the PATH by default (might depend on configuration), so your script will exit without giving any info on the problem, and it quite often ends up being that it can't find the program, in this case python. So change it to:
shell_exec("/full/path/to/python /full/path/to/my/script");
If your python script is running on it's own without problems, then it's very likely this is the problem. As for the memory, I'm pretty sure PHP won't use the same memory python is using. So if it's using 300MB PHP should stay at default (say 1MB) and just wait for the end of shell_exec.
A proplem could be that your script takes longer than the server waiting time definied for a request (can be set in the php.ini or httpd.conf).
Another issue could be that the servers account does not have the right to execute or access code or files needed for your script to run.
Found this before and helped me solve my background execution problem:
function background_exec($command)
{
if(substr(php_uname(), 0, 7) == 'Windows')
{
pclose(popen('start "background_exec" ' . $command, 'r'));
}
else
{
exec($command . ' > /dev/null &');
}
}
Source:
http://www.warpturn.com/execute-a-background-process-on-windows-and-linux-with-php/
Thanks for your answers, but none of them worked :(. I decided to implement in a dirty way, using busy waiting, instead of triggering an event when a record is inserted.
I wrote a backup process that runs forever and at each iteration checks if there is something new in database. When it finds a record, it executes the script and everything is fine. The idea is that I launch the backup process from the shell.
I found that the issue when I tried this was the simple fact that I did not compile the source on the server I was running it on. By compiling on your local machine and then uploading to your server, it will be corrupted in some way. shell_exec() should work by compiling the source you are trying to run on the same server your are running the script.