参见脚本:
#!/bin/bash
# HLDS所存放的目录
export HLDS_BASE=/usr/games/
if [ -z "$1" ] || [ -z "$2" ]; then
        # 语法格式:cs.sh 要操作的服务器(1、2、3、4、5号服务器或全部) 要执行的操作(开始、停止、重启、查看控制台)
        echo "Usage:"
        echo "        cs.sh 1|2|3|4|5|all  start|stop|restart|view"
        exit 1
fi
CMD=""
NAME=""
case "$1" in
1)
        # 操作1号服务器
        cd $HLDS_BASE/1F
        NAME="server1"
        CMD="screen -A -m -d -S "$NAME" taskset -c 2 ./hlds_run -tos -binary ./hlds_i686 -timeout 3 -pingboost 2 -game cstrike  +ip 0.0.0.0 -port 27015 +map de_dust2 +maxplayers 32 -insecure +sv_lan 0 -nojoy -noipx"
        ;;
2)
        # 操作2号服务器
        cd $HLDS_BASE/2F
        NAME="server2"
        CMD="screen -A -m -d -S "$NAME" taskset -c 3 ./hlds_run -tos -binary ./hlds_i686 -timeout 3 -pingboost 2 -game cstrike  +ip 0.0.0.0 -port 27016 +map de_dust2 +maxplayers 30 -insecure +sv_lan 0 -nojoy -noipx"
        ;;
3)
        # 操作3号服务器
        cd $HLDS_BASE/3F
        NAME="server3"
        CMD="screen -A -m -d -S "$NAME" taskset -c 4 ./hlds_run -tos -binary ./hlds_i686 -timeout 3 -pingboost 2 -game cstrike  +ip 0.0.0.0 -port 27017 +map cs_bloodstrike +maxplayers 18 -insecure +sv_lan 0 -nojoy -noipx"
        ;;
4)
        # 操作4号服务器
        cd $HLDS_BASE/4F
        NAME="server4"
        CMD="screen -A -m -d -S "$NAME" taskset -c 5 ./hlds_run -tos -binary ./hlds_i686 -timeout 3 -pingboost 2 -game cstrike  +ip 0.0.0.0 -port 27018 +map de_dust2 +maxplayers 32 -insecure +sv_lan 0 -nojoy -noipx"
        ;;
5)
        # 操作5号服务器
        cd $HLDS_BASE/5F
        NAME="server5"
        CMD="screen -A -m -d -S "$NAME" taskset -c 6 ./hlds_run -tos -binary ./hlds_i686 -timeout 3 -pingboost 2 -game cstrike  +ip 0.0.0.0 -port 27019 +map de_dust2 +maxplayers 32 -insecure +sv_lan 0 -nojoy -noipx"
        ;;
all)
        # 操作所有服务器
        if [ "$2" = "view" ]; then
                screen -ls
                exit 0
        fi
        $0 1 $2
        $0 2 $2
        $0 3 $2
        $0 4 $2
        $0 5 $2
        exit 0
        ;;
*)
        # 如果第一个参数不是以上几个则打印使用帮助
        echo "Usage:"
        echo "        cs.sh 1|2|3|4|5|hltv|all  start|stop|restart|view"
        exit 3
esac
case "$2" in
start)
        echo "Starting "$NAME" ..."
        screen -ls | grep $NAME | grep tached
        if [ $? -eq 0 ]; then
                  echo ERROR: $NAME has already started!
                exit 2
        else
                $CMD
        fi
        ;;
stop)
        echo "Shutdown "$NAME" ..."
        screen -dr $NAME -X quit
        ;;
restart)
        echo "Restart "$NAME" ..."
        screen -dr $NAME -X quit
        $CMD
        ;;
view)
        echo "View "$NAME" console ..."
        screen -dr $NAME
        ;;
*)
        # 如果第二个参数不是start、stop、restart、view则打印使用帮助
        echo "Usage:"
        echo "        cs.sh 1|2|3|4|5|hltv|all  start|stop|restart|view"
        exit 4
esac
exit 0