gpt4 book ai didi

php - 在后台进程中运行 bash 文件问题

转载 作者:太空宇宙 更新时间:2023-11-04 03:33:35 24 4
gpt4 key购买 nike

我创建了一个简单的 bash 文件来运行这样的 php 文件:

ws.sh

!#/bin/bash
php -q /var/www/htdocs/server.php

服务器.php

<?php
// prevent the server from timing out
set_time_limit(0);
session_start();
// include the web sockets server script (the server is started at the far bottom of this file)
require 'class.PHPWebSocket.php';

// when a client sends data to the server
function wsOnMessage($clientID, $message, $messageLength, $binary) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );

// check if message length is 0
if ($messageLength == 0) {
$Server->wsClose($clientID);
return;
}

$_SESSION['currentId'] = $message;
}

// when a client connects
function wsOnOpen($clientID){
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );
$Server->log( "$ip ($clientID) has connected. ");
$data = array('connectionId' => $clientID,'msg'=>'Connected');
$Server->wsSend($clientID, json_encode($data));

}

// when a client closes or lost connection
function wsOnClose($clientID, $status) {
global $Server;
$ip = long2ip( $Server->wsClients[$clientID][6] );

$Server->log( "$ip ($clientID) has disconnected. sess = ".$session_id );

}

// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');

$Server->wsStartServer('server-ip-here', 81);

?>

然后在我的终端上将 bash 文件运行到后台

./ws.sh &

现在,问题是,如果在后台运行的 bash 文件,客户端无法连接到我的 websocket 并返回错误“连接被拒绝”,但如果它正常运行(不是后台),我在连接到它时就会遇到问题。任何想法?请帮忙。谢谢!

最佳答案

已经弄清楚了。我只是将 php 命令行更改为:

php -q /var/www/htdocs/server.php >/dev/null 2>&1 &

关于php - 在后台进程中运行 bash 文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32389834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com