English 中文(简体)
Run and forget system call in php
原标题:

So I am trying to execute some script from my php code. That lives in page blah.php

<?php
     // ....
     // just basic web site that allows upload of file...
?>

Inside I use system call

if (system("blah.pl arg1") != 0)
{
   print "error
";
}
else
{
   print "working on it..you will receive e-mail at completion
";
}

Works great but it waits until it completes until it prints working on it. I am aware that I am calling perl script from php not a typo.

How can I just start program execution and let it complete in background. The blah.pl script handles e-mail notification.

Anyone?

Thanks I appreciate it

最佳回答

From system function documentation:

Note: 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.

Simply redirect the output of your Perl script and PHP should be able to run without having to wait for the script to finish.

system("blah.pl arg1 > /dev/null 2>&1 &");
问题回答

Can you add an ampersand to spawn the process in the background?

if (system("blah.pl arg1 &") != 0)

You could probably use the popen function

http://www.php.net/manual/en/function.popen.php

Since all the PHP command line functions wait for a result - I recommend that you have a separate page to call the emailer script.

In other words, send a request to the email.php page (or whatever) using cURL or the file wrappers which allows you to close the connection before receiving a result. So the process will look something like this.

        page_running.php
        |
    cURL call to (email.php?arg1=0)
        |           |
final output        email.php
                    |
                    calls system("blah.pl arg1")




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签