BF2142サーバークエリスクリプトの改良

gamedig bash スクリプト

の続編です。主な変更点は、バッファリングによりサーバー側の負荷を低減し、サーバーダウン時に無駄なログを残さず、サーバーにプレイヤーが居る時だけログを更新する等です。最終形かな。

スクリプトそのものを示します。

nao@ghost:~$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
0,30 4,5,6 * * * /home/nao/scr.sh >> /mnt/nas/images/diff.log 2> /dev/null
38 6 */6 * * /home/nao/ping2.sh
38 4 */6 * * /home/nao/ping3.sh
38 5 */6 * * /home/nao/ping.sh
#*/10 * * * * /home/nao/kor >> /mnt/nas/server-pop/entry.csv 2> /dev/null
*/5 * * * * /home/nao/pdown >> /mnt/nas/server-pop/entry.csv 2> /dev/null
0 0 * * * /home/nao/dispcounters/add.sh 2> /dev/null
#5 0 * * * /home/nao/beacon

pdownの内容は、

#!/bin/bash

first=`date '+%Y/%m/%d %H:%M:%S'`
once=$(/usr/local/bin/gamedig --socketTimeout 1000 --type battlefield2142  192.168.0.201:29900)

if [[ "$once" == *error* ]]; then
        :
else
zero=$(echo $once | /usr/bin/jq -r '.numplayers')

if [ $zero != "0" ]; then

second=$(echo $once | /usr/bin/jq  -r '[.map,.numplayers] | join(",")')
third=$(echo $once | /usr/bin/jq -r '.players[] | select(.raw.pid != 0)' | jq -r '.name' )

if [ -n "$third" ]; then
        echo $first,$second,$third
else
        echo $first,$second
fi
fi
fi

出力例は、

2026/03/21 04:50:01,Liberation_of_Leipzig_coop,1, agathyus
2026/03/21 04:55:02,Liberation_of_Leipzig_coop,1, agathyus
2026/03/21 06:20:01,Camp_Gibraltar_coop,1, agathyus
2026/03/21 06:25:01,Camp_Gibraltar_coop,1, agathyus
2026/03/21 06:30:01,Camp_Gibraltar_coop,1, agathyus
2026/03/21 06:35:01,Camp_Gibraltar_coop,1, agathyus
2026/03/21 06:40:01,Camp_Gibraltar_coop,1, agathyus
2026/03/21 11:35:02,Minsk_coop,1, jake_burst
2026/03/21 11:40:01,Minsk_coop,1, jake_burst
2026/03/21 11:45:01,Tunis_Harbor_coop,1, jake_burst
2026/03/21 15:10:01,Air_Bavaria,1, jake_burst
2026/03/21 15:15:01,Air_Bavaria,1, jake_burst
2026/03/21 15:20:01,Fall_of_Berlin_coop,1, jake_burst
2026/03/21 16:30:01,Bridge_at_Remagen_coop,1, jake_burst
2026/03/21 16:35:01,Bridge_at_Remagen_coop,1, jake_burst
2026/03/21 16:40:01,Bridge_at_Remagen_coop,1, jake_burst
2026/03/21 22:35:01,Belgrade_coop,1, agathyus
2026/03/21 22:40:01,Belgrade_coop,1, agathyus
2026/03/21 22:45:01,Belgrade_coop,1, agathyus
2026/03/21 22:50:01,Belgrade_coop,1, agathyus
2026/03/21 22:55:01,Belgrade_coop,1, agathyus
2026/03/22 01:35:01,Highway_Tampa_coop,1, agathyus
2026/03/22 01:40:01,Highway_Tampa_coop,1, agathyus
2026/03/22 01:45:01,Highway_Tampa_coop,1, agathyus
2026/03/22 01:50:01,Highway_Tampa_coop,1, agathyus
2026/03/22 08:40:01,Suez_Canal_coop,1, jake_burst
2026/03/22 08:45:01,Suez_Canal_coop,1, jake_burst

jake_burstというのは筆者です。

コメント