繁体   English   中英

在后台运行PHP脚本(不使用Cron)

[英]Run PHP Script in Background(Without Using Cron)

我想在某些情况下在后台运行以下代码:

文件A:

// ------------------- Some Code Before Following -----------------------

// send notification to given device token
$notification = new Notification();                    
$notification->sendNotification($token,$count);

// ------------------- Some Code After Above ------------------------

这将称为以下课程:

// Here I am sending notification to given device token (APNS Push Notification) 
<?php

class Notification
{
    public function sendNotification($token,$count)
    {
        if(strlen($token)%16 == 0)
        {
            // set Device token
            $deviceToken = $token;
            $passphrase = 'xxxxx';
            $badge = $count;

            // Displays alert message here:
            $message = 'Hello! There.';

            $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
            stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

            // Open a connection to the APNS server
            $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
            if (!$fp)
                exit("Failed to connect: $err $errstr" . PHP_EOL);

            echo("Connected to APNS.");

            // Create the payload body
            $body['aps'] = array(
                                 'alert' => $message,
                                 'badge' => $badge,
                                 'sound' => 'default'
                                 );

            // Encode the payload as JSON
            $payload = json_encode($body);

            // Build the binary notification
            $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

            // Send it to the server
            $result = fwrite($fp, $msg, strlen($msg));

            if (!$result)
                echo("\n\nMessage not delivered.\n\n");
            else
                echo("\n\nMessage successfully delivered.\n\n");

            // Close the connection to the server
            fclose($fp);
        }
    }
}


?>

当此类的Execution completed it will go back to file Acontinue execution after sending notification.

但是在这里take time to send notification (around 3-5-7 seconds)take time to send notification (around 3-5-7 seconds) ,在这段时间内user has to wait which is not a good idea

所以我想做的是send notification in Background process instead of main threadsend notification in Background process instead of main thread 。这样用户就不必不必要地等待。

Please note that I don't want to use Cron.

我发现exec/shell_exec/passthru command可以为我提供帮助。 但是在这种情况下,我out of these three commands中使用out of these three commands哪个命令?

请为此指导我。 任何帮助将不胜感激。

passthru("/usr/bin/php /path/to/yourFile.php >> /path/to/log_file.log 2>&1 &");

这里有一些重要的事情。

首先:将完整路径放置到php二进制文件中,因为此命令将在apache用户下运行,并且您可能没有在该用户中设置类似php的命令别名。

第二步:请注意命令字符串末尾的两件事: 2>&1& 2>&1用于将错误重定向到标准IO。 最重要的是命令字符串末尾的& ,它告诉终端不要等待响应。

这里有一些选择:

  • 您可以使用pcntl_fork( http://php.net/manual/en/function.pcntl-fork.php )分叉一个单独的PHP进程。
  • 您可以使用cronjob来检查您不想删除的未发送的修改(您未提及WHY,这就是为什么我提到它)
  • 您可以为其使用作业队列系统。 到目前为止,这是最好的解决方案,但需要一些时间才能上手。 Gearman是我经验丰富的人,它的工作原理就像一种魅力。

请按照此处的说明在后台运行PHP脚本:

php执行后台进程

它需要使用shell_exec从文件A调用脚本,因此您将不得不为要调用的代码创建一个新文件。 它还假设您正在使用linux

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM