PHP: exec() doesn't run in the background even with “>/dev/null 2>&1 &”

前端 未结 7 984
迷失自我
迷失自我 2021-01-04 10:59

I\'m calling this in my php script:

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

Calling the script (generates a pdf and s

相关标签:
7条回答
  • 2021-01-04 11:40

    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.

    0 讨论(0)
  • 2021-01-04 11:47

    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 &')
    
    0 讨论(0)
  • 2021-01-04 11:49

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

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

    0 讨论(0)
  • 2021-01-04 11:51
        /**
         * @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

    0 讨论(0)
  • 2021-01-04 11:54

    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.

    0 讨论(0)
  • 2021-01-04 11:55

    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 &');
    
    0 讨论(0)
提交回复
热议问题