0

我正在尝试修改我编写的这个非常简单的 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。我怎样才能做到这一点?

4

1 回答 1

1

要检查屏幕是否已经在运行,我通常只是从以下位置对其进行 grep screen -ls

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
于 2012-07-17T17:13:14.563 回答