首页 > 解决方案 > 读取进程(游戏服务器)的控制台输出,并在出现“加入”一词时执行一些操作

问题描述

我试图设置一个 linux l4d2 游戏服务器,当有人加入大厅时,它会向我的 teampeak 发送消息。不知何故,我无法弄清楚我如何在 bash 中运行该进程以读取其内容并在有人加入时捕获。游戏服务器的输出在一行中清楚地写着“XXXX加入了游戏”

Bash 读取输出? 不知何故,这是行不通的。它冻结了这个过程。

output=$(./srcds_run)
while read -r line; do
    process "$line"
        if [ $line = "XXXX joined" ]; then
                echo "it works";
        fi
done <<< "$output"

当我运行它来启动服务器时,它会在某个时候挂起并且没有启动。

编辑1:

srcds_run 的输出:

$ ./startServer.sh
Setting breakpad minidump AppID = 222860
Using breakpad crash handler
Forcing breakpad minidump interfaces to load
dlopen failed trying to load:
/home/steam/.steam/sdk32/steamclient.so
with error:
/home/steam/.steam/sdk32/steamclient.so: cannot open shared object file: No such file or directory
Looking up breakpad interfaces from steamclient
Calling BreakpadMiniDumpSystemInit
[S_API FAIL] SteamAPI_Init() failed; SteamAPI_IsSteamRunning() failed.
[S_API FAIL] SteamAPI_Init() failed; unable to locate a running instance of Steam, or a local steamclient.so.
[S_API FAIL] SteamAPI_Init() failed; SteamAPI_IsSteamRunning() failed.
Setting breakpad minidump AppID = 550
dlopen failed trying to load:
/home/steam/.steam/sdk32/steamclient.so
with error:
/home/steam/.steam/sdk32/steamclient.so: cannot open shared object file: No such file or directory
Looking up breakpad interfaces from steamclient
Calling BreakpadMiniDumpSystemInit
Setting breakpad minidump AppID = 222860

-- Here is where srcds_run freeze --

这是用户“XXXX”连接和断开大厅时 srcds_run 的完整输出:

Client "XXXX" connected (127.0.0.1:27005).
Server waking up from hibernation
Initiating Reserved Wanderers
ConVarRef mat_hdr_manual_tonemap_rate doesn't point to an existing ConVar
String Table dictionary for soundprecache should be rebuilt, only found 9751 of 16341 strings in dictionary
String Table dictionary for Scenes should be rebuilt, only found 6770 of 13181 strings in dictionary
NextBot tickrate changed from 0 (0.000ms) to 3 (0.100ms)
Dropped XXXX from server (Disconnect by user.)
Server is hibernating

标签: linuxbashprocessscriptingdebian

解决方案


从我从问题的评论和更新中收集到的,srcds_run是一个持续运行的过程/脚本。
所以output=$(./srcds_run)在你的游戏结束之前不会终止。(这是我的看法;可能是错误的。)
但是,如果它是正确的,你可以试试这个:

$ ./srcds_run | grep --line-buffered "XXXX joined" | while read _; do
    echo it works
done

顺便说一句,从您的输出来看,您的游戏启动似乎失败了,因为它无法找到 Steam 库。


推荐阅读