12

I'm calling this in my php script:

    exec("gutschein.php >/dev/null 2>&1 &");

Calling the script (generates a pdf and sends it away by e-mail) works, but the process is not running in the background (I checked it out with a sleep statement inside gutschein.php). The browser is hanging until execution of gutschein.php is finished.

I also checked out the following:

    exec("/usr/bin/php gutschein.php >/dev/null 2>&1 &");

or

shell_exec("/usr/bin/php gutschein.php >/dev/null 2>&1 &");

It doesn't change anything. The script is actually running on a linux server. Has anybody an idea what I'm doing wrong?

9
  • If I remember correctly, when you run a command with a & from a shell you still need to press a key to get the prompt back. Wouldn't it be the same for PHP? Commented Sep 18, 2012 at 12:20
  • 2
    @MathieuImbert: You remember incorrectly. Commented Sep 18, 2012 at 12:22
  • better aproach to this problem would be to create a query with commands (tasks) to run, and another process (in.e. run from CRON) could consume the quey and execute commands ony by one... Commented Sep 18, 2012 at 12:23
  • check with pass_through also.. Commented Sep 18, 2012 at 12:23
  • try to fork first, then exec ? Commented Sep 18, 2012 at 12:27

7 Answers 7

4

Try system and/or passthru. I've had issues with exec before because it halts trying to fill the return array with data until the process has finished.

These will both echo raw output so even if they work you may need to handle that with a discarded output buffer.

Sign up to request clarification or add additional context in comments.

Comments

2
    /**
     * @author Micheal Mouner
     * @param String $commandJob
     * @return Integer $pid
     */
    public function PsExec($commandJob)
    {
        $command = $commandJob . ' > /dev/null 2>&1 & echo $!';
        exec($command, $op);
        $pid = (int) $op[0];
        if ($pid != "")
            return $pid;
        return false;
    }

This worked for me .. check it

also, return processId of the background process

Comments

1

All output must be redirected, else the script will hang as long as gutschein.php executes. Try

exec('/usr/bin/php gutschein.php &> /dev/null &');

1 Comment

i'm already redirecting: ">/dev/null 2>&1". I checked out your suggestion, but it doesn't work
1

Can you try one of the following 2 commands to run background jobs from PHP:

$out = shell_exec('nohup /usr/bin/php /path/to/gutschein.php >/dev/null 2>&1 &');

OR

$pid = pclose(popen('/usr/bin/php gutschein.php', 'r'));

It will execute the command in background and returns you the PID, which you can check using condition $pid > 0 to ensure it has worked.

5 Comments

the script is executed. that is not the problem. the problem is that its not executed in the background...
I have tried above commands via a browser and it was indeed executed in background.
i checked out your 2 suggestions. they execute the script like a charm but unfortunately not in the background...in my case it doesn't work.
I find it strange since I am able to run above script in background in my browser (not on command line). I suspect there is some php.ini setting on your host that is preventing PHP executing a background job.
maybe. but they should know actually... could the problem have something to do with "Output Buffering?
1

You can use screen: screen -d -m /usr/bin/php gutschein.php

Here is screen's manual if you need more info on options.

Comments

1

Please see my experience at HERE. The way I try and works for me -

php -q /path/to/my/test/script/cli_test.php argument1 < /dev/null &

In PHP, it is like

exec('php -q /path/to/my/test/script/cli_test.php argument1 < /dev/null &')

Comments

-3

The browser is might waiting for a response, so let your script produce any output and terminate the "main process". a simple

die('ok');

should do the job.


btw, forking a new process by calling exec is might not the best solution since the new process isn't a real child process - means you can't control it. you might consider using pcntl (http://php.net/manual/de/book.pcntl.php) for this purpose. but stand clear, this extension has also his pitfalls, especially in the context of a webserver.

2 Comments

this doesn't help and my hoster doens't allow me to use the pcntl-functionality, i.e. the extension is not available.
This dont help, is not only kill the proccess.

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.