If i run shell_exec(php file) , will it activate the shell_execution and continue with the php file, or will it try to complete everything in the shell_executed php file first, then run the rest of the php file that executed it.
It will complete the shell execution first and then it will run rest of the code in the php file.
shell_exec(), as stated in the Documentation, will return the complete output as a string. So it has to be a "blocking" function. That means it will block the execution of the rest of your code until it is complete.
Depending on your command that you want to execute, you may want to force the process to run in the background with the & character at the end of the command. This is assuming ofcourse that you are running on a unix based server.
Related
I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec 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.
Please refer this link for your version of PHP: manual
I am actually writing a little PHP script, in order to retrieve files in folder on a FTP server and download them locally.
Another script put those file in the server.
So here's my question : how can I write a batch file that will execute the first php script, then run a program (a game in this case).
When the game is closed, execute the second script.
So it will be this process :
When run the batch file, execute the first script (get file)
When all files are downloaded, run the programm (game.exe)
After game closing, execute the second script (put file)
I think at a .bat file, but maybe there is an another solution.
Thanks is advance !
Telest.
EDIT :
Thanks for your answer.
Google gives me this post : How to wait for a process to terminate to execute another process in batch file.
I found that I can use /W in order to wait the program ends.
But /W seems not working. Tried /WAIT but no success.
Here is my batch file :
PHP C:\Users\PHProjects\test1.php
START /WAIT /B chrome.exe
PHP C:\Users\PHProjects\test2.php
PAUSE
(test1.php & test2.php are just dummy echo).
And the console result :
Chrome executes well, but test2 too
As you can see, Chrome executes well, but so do test2.php.
Is something wrong here ?
Thanks.
call getfile.bat
start "" /w game.exe
call putfile.bat
see start /? and call /?. With start take note of the second half which details how not using start works (confusing I know}. Call details, as start details for programs, how it works calling a batch directly versus call.
I updated my first post with new elements.
I'm trying to write a cronjob which launches multiple processes that I want to run in parallel.
I'm using a foreach calling each command, but the command line waits for the output. I don't want it to put.
Was wondering if anyone ever used any library for this?
Add an ampersand after the command:
$ php task.php &
It will run that instance of php in the background and continue.
If you read the manual on passthru you'll notice it tells you how to avoid this...
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.
So you can rely on UNIX fds to redirect output to something like /dev/null for example if you don't care about the output or to some file if you do want to save the output and this will avoid PHP waiting on the command to finish.
pssthru("somecommand > /some/path/to/file")
I have two php files. In one php file there is simple html form in which I created some drop down for select time and days for cronjob when user set time and day and submit form then all the drop down values stored in database.
Now with the help of this stored values I need to set cronjob and when cron will execute then it run another php file in which I write some code for generate xml file.
And this file run every time which time is stored by user in database.
By using Cpanel I can do that but I don't want to use cpanel.
Please give me some proper and working solution, I really need it.
Thanks in advance.
Use phps system() or exec() function to call the crontab command to replace or modify the existing crontab for the account the web server runs under. You might have to make sure that user is allowed to use the cron system, this depends on the operating system you use.
From within the crontab you can use one of two strategies to run a php script_
- call the php cli interpreter like any normal command, so something like: /usr/bin/php and give it the script to interpret. As an alternative you also can use shebang inside your php script and call it as a simple executable.
- use wget to call an url pointing to your webserver (maybe localhost) when you want to execute the script inside your web server.
This might act as a starting point for you to experiment with:
#!/usr/bin/php
<?php
// the time tokens to be fed into the crontab line
$time=array('10','*','*','*','*');
// the actual command to be executed
$command=sprintf("crontab -l | (cat;echo \"%s\t%s\") | crontab",
implode(' ',$time),
'/usr/bin/beep');
// execute command
$result=system($command);
// output result of execution
if ($result)
echo "Result: $result\n";
else
echo "FAILURE!\n";
?>
Works for me when called from CLI.
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