【问题标题】:Read the console output of a process (gameserver) and make some actions when a word appear "join"读取进程(游戏服务器)的控制台输出,并在出现“加入”一词时执行一些操作
【发布时间】:2019-08-09 07:33:22
【问题描述】:

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

Bash read output? 这不知何故是行不通的。它冻结了这个过程。

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

【问题讨论】:

  • 尝试将if [ $line = "XXXX joined" ]; then更改为if [ "$line" = "XXXX joined" ]; then
  • srcds_run 返回什么?请使用srcds_run 的输出更新问题。另外,命令srcds_run在运行后是否立即结束?
  • 抱歉回复晚了。 @anishsane 我按照您在第一条评论中所说的做了,但没有用。我还用 srcds_run 的输出更新了问题。

标签: linux bash process scripting debian


【解决方案1】:

http://forums.srcds.com/viewtopic/4446

您可以将输出写入日志文件并使用 aka:tail -f /logfile|if [ "$(grep "XXXX joined")" ];然后回显“它有效”;菲

【讨论】:

  • 抱歉回复晚了。那是行不通的。我制作了另一个脚本来读出日志文件:tail -f left4dead2/logs/L000_000_000_000_0_201908110858_000.log|if [ "$(grep "STEAM USERID")" ];然后回显“它有效”;菲
  • 但我没有得到结果。在控制台中我让 XXX 加入了游戏,但在日志文件中有所不同。所以我也调整了这个。这就是我从日志文件中得到的信息:L 08/11/2019 - 09:04:41:世界触发了“Round_Start” L 08/11/2019 - 09:04:41:“XXX<0: xxxxxxx><0:xxxxxxx>
【解决方案2】:

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

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

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

【讨论】:

  • 非常感谢这样的作品。唯一的问题是,在我断开连接后,我只能以某种方式得到“它有效”。不用担心输出的错误信息,它无论如何都可以工作。
  • 如果它对您有用,请接受答案(也可以选择投票),以便问题可以解决。
  • 能否粘贴srcds_run的输出的sn-p,包括XXXX joined?需要查看这些行是否确实是换行符终止的。
  • 我会在它工作时接受答案。当有人加入时,我用 srcds_run 的输出更新了问题。请记住,我使用上面的代码,但不是“加入 XXXX”,而是使用“客户端”,据说它有点工作,但不知何故只有当我断开连接时。这对我来说毫无意义。
  • 忽略我的脚本。只需在终端上启动./srcds_run,然后在编写脚本之前手动监控输出。当用户连接时,但在断开连接之前,您会看到什么?也许这个脚本/应用程序在用户断开连接之前不会打印任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多