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.

fmarinheiro
  • 77
  • 3
  • 8

2 Answers2

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");
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
  • 1
    more elegant way - http://stackoverflow.com/questions/45953/php-execute-a-background-process – ajreal Nov 10 '10 at 13:00
  • I think that's overkill (= not more elegant: undue files etc.) unless you actually need to monitor the process. – Joonas Pulakka Nov 10 '10 at 13:32
  • `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 Nov 10 '10 at 13:45
  • @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. – Joonas Pulakka Nov 10 '10 at 13:55
  • 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] – ajreal Nov 10 '10 at 14:46
  • The `&` is intended for *shell* use. I don't think that `exec()` runs its commands in any particular shell; it just starts the given program as a new process using the given arguments, possibly redirecting the output somewhere. Again, I may be wrong. – Joonas Pulakka Nov 10 '10 at 15:05
  • `&` is for all inux command including callable php executable binary, happening i doing some testing on background processing using PHP, but the example u provided have to wait until it finished. The example that i been using is crawl a site using wget where i already has list of the URL on database. – ajreal Nov 10 '10 at 15:25
  • `&` is a *shell* command that tells the *shell* to put the job in the background. From what you describe, it seems that `exec()` either runs its arguments within some shell, or then it parses its arguments and just behaves like a shell would. Either way, it seems that its documentation is somewhat fuzzy (but hey, it's PHP). – Joonas Pulakka Nov 10 '10 at 16:25
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
?>
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • 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 – fmarinheiro Nov 10 '10 at 10:06
  • My Apologise, I forgot to add `/dev/null` so it does not wait. – RobertPitt Nov 10 '10 at 12:57