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

时间:2015-09-04 03:58:44

标签: php linux websocket

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

ws.sh

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

server.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并返回错误“CONNECTION REFUSED”但是如果它正常运行(不是后台)我必须连接到问题它。任何的想法?请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

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

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