用于在屏幕中启动游戏服务器的Shell脚本

时间:2012-07-17 16:01:26

标签: shell

我正在尝试修改我编写的这个非常简单的shell脚本,以检查屏幕是否已经处于活动状态,如果是,则不创建它们。例如,如果我使用start参数调用此脚本两次,则将进行四次屏幕会话。我想阻止它。

#! /bin/sh
# /etc/init.d/css-server
#

case "$1" in
  start)
    echo "Starting Nullus Imprimis war server..."
    screen -A -m -d -S css-war-server /home/css-servers/war-server/css/srcds_run -game cstrike +map de_dust2 +maxplayers 16 -autoupdate -port 2555
    echo "Nullus Imprimis war server started"
    echo "Starting Nullus Imprimis pub server #1..."
    screen -A -m -d -S css-pub-server-1 /home/css-servers/pub-server-1/css/srcds_run -game cstrike +map de_dust2 +maxplayers 32 -autoupdate -port 2666
    echo "Nullus Imprimis pub server #1 started"
    ;;
  stop)
    echo "Stopping Nullus Imprimis war server..."
    screen -S css-war-server -X quit
    echo "Nullus Imprimis war server stopped"
    echo "Stopping Nullus Imprimis pub server #1..."
    screen -S css-pub-server-1 -X quit
    echo "Nullus Imprimis pub server #1 stopped"
    ;;
  *)
    echo "Usage: service css-servers {start|stop}"
    exit 1
    ;;
esac

exit 0

另外,我想让服务器在他们自己的用户名下运行,在本例中为css-servers。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

要检查屏幕是否已在运行,我通常只是从screen -ls

进行grep
screen -ls | grep -q NAME || ...do something if server is not running...

或者:

if ! screen -ls | grep -q NAME; then
  ...do something if server is not running...
fi

为了以不同的用户身份运行它,我建议使用sudo -u运行启动脚本,如下所示:

sudo -u css-servers STARTUP_SCRIPT