I'm trying to fire a php script inside another php script.
This is easy, the problem is that the first script can not wait for the second to finish.
I want a fire and go mechanism.
Any help would be appreciated,
Thanks in advance.
From exec documentation:
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.
That is, the following should work:
exec("php /path.to.file.php > /dev/null");
You would have to use exec()
On your server / Operating system add the php/bin directory to your environment variables and then execute the command like so:
<?php
//Blah
exec("php /path.to.file.php /dev/null");
//Blah
?>
Related
When executing multiple scripts within PHP using the exec command; are each script ran one at a time as in one after the other or are they ran simultaneously?
exec('/usr/bin/php -q process-duplicates.php');
exec('/usr/bin/php -q process-images.php');
exec('/usr/bin/php -q process-sitemaps.php');
Just want to make sure they are one after the other before attempting to rewrite my crontabs.
Sure, the only way to run at background is adding & to the command line arguments, which would put that exec()'d process into the background:
exec("php test.php &");
So you are right, they run one after the other.
NOTE: In your case you shouldn't use & as it will force to run all the scripts simultaneously.
exec waits for the script to return, see php.net
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.
But as a devops, please please please do not run your cron jobs like this! Create entries in the crontab for each, or put them in a shell script and have cron run the script.
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 noticed exec and shell_exec is causing perpetual loading.
Basically, I'm trying to do something as simple as loading a PHP script in the background. When I try to do that, it just loads and loads.
My code is as follows
exec('php test.php -- '.escapeshellarg($param1).' > /dev/null ');
I first thought it was my other script, so I pointed it to a file with just:
echo $agrv[1];
But it still loads perpetually.
Don't wait for the process to exit
exec() waits for the process to give an exit code. The link I provided above may help you.
Oh, and since you tagged Linux for whatever reason, I assume you're on a Linux distro.
You could consider this, aswell:
http://ca1.php.net/pcntl_fork
I have written php program and uploaded on server. I want run this program infinitely. My programs source is like this:
<?php
while (1<2){
make something;
}
?>
Of course, if i will open this page in my browser it will run, but if i will shut down my pc it will stop working. How i can run this program infinitely without opening in any browser.
Do this:
<?php
set_time_limit(0);
ignore_user_abort(true);
while(true) {
//Do something
}
?>
But it's a very very very bad idea to do that without a very very very good reason.
You might run that kind of script in CLI and use a SIGINT or a SIGKILL to be sure stopping your script without rebooting your apache server... (Why I just explain that? Don't do it man, it's dangerous...)
Run in commandline or run as a cronjob; you can also check for making a php file a system daemon:
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/
Using PHP as a daemon allows you to make it run indefinitely, however you might have to reset it at regular intervals to ensure it does not stack memory.
By the way:
while( true )
does also work.
You can try this -
<?php
while(1) {
"some code"
}
?>
you could start the script with popen() which starts a new commandline process. So you would start a CLI PHP with the desired script.
You can use deamons (service), must run script. description here
I encountered this same problem for running a java program infinitely on a linux server.
I solved the problem by using the linux 'screen' command, instructions found here
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