1

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.

2 Answers 2

2

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");
Sign up to request clarification or add additional context in comments.

8 Comments

I think that's overkill (= not more elegant: undue files etc.) unless you actually need to monitor the process.
exec("php /path.to.file.php > /dev/null"); is not really work (or i mistaken?), let's said the path.to.file.php <? sleep(20); ?>, end up 1st script need to wait 20 seconds before 2nd script completed
@ajreal: According to PHP documentation (as quoted above), redirecting the output stream is sufficient for the program to run in the background. Apparently PHP always starts a separate process when using exec(), and waits there only if the output stream has not been redirected elsewhere. The documentation could be wrong, of course. At least it's not quite crystal clear.
yes, i assume u need to use exec("php /path.to.file.php > /dev/null &"); or exec('nohup php /path.to.file.php &'); [running as linux+cli]
|
1

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
?>

2 Comments

I have already tried exec but the script will wait for 'path.to.file.php' script to terminate and than continue. I want to continue right after launching. thnaks
My Apologise, I forgot to add /dev/null so it does not wait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.