2012-07-17 103 views
0

我試圖修改這個非常簡單的外殼腳本,我寫了這個腳本來檢查屏幕是否已經激活,如果是,請不要創建它們。例如,如果我使用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

要檢查屏幕已經在運行,我通常只是從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