2016-09-06 197 views
1

在CentOS 6.8中,我有一個golang應用程序,它在命令go run main.go中運行,我需要創建一個系統服務以便像引導服務httpd一樣運行它。
我知道我必須創建類似/etc/rc.d/init.d/httpd的文件但我不知道如何執行該命令。通過服務運行應用程序

+0

這應該有助於指導您:http://unix.stackexchange.com/questions/236084/how-do-i-create-a-service-for-a-shell-script-so-i-can-start-and -stop-it-like-ad – AJPennster

+0

Golang app的意思是web應用程序或系統應用程序? –

+0

@KyawMyintThein網絡應用 –

回答

0

首先,您需要構建您的二進制文件並放在您的路徑下。

go install main.go 

這並不是說,如果你的「主」文件被稱爲主,go install將放置您的路徑下稱爲主二進制文件。所以我建議你把你的文件重命名爲你的項目/服務器。您可以運行coolserver以確保一切正常。我會,如果你得到你$ GOPATH安裝正確。在這裏,它是一個的init.d服務的一個例子稱爲service.sh

#!/bin/sh 
### BEGIN INIT INFO 
# Provides:   <NAME> 
# Required-Start: $local_fs $network $named $time $syslog 
# Required-Stop:  $local_fs $network $named $time $syslog 
# Default-Start:  2 3 4 5 
# Default-Stop:  0 1 6 
# Description:  <DESCRIPTION> 
### END INIT INFO 

SCRIPT=<COMMAND> 
FLAGS="--auth=user:password" 
RUNAS=<USERNAME> 

PIDFILE=/var/run/<NAME>.pid 
LOGFILE=/var/log/<NAME>.log 

start() { 
    if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then 
    echo 'Service already running' >&2 
    return 1 
    fi 
    echo 'Starting service…' >&2 
    local CMD="$SCRIPT $FLAGS &> \"$LOGFILE\" & echo \$!" 
    su -c "$CMD" $RUNAS > "$PIDFILE" 
    echo 'Service started' >&2 
} 

stop() { 
    if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then 
    echo 'Service not running' >&2 
    return 1 
    fi 
    echo 'Stopping service…' >&2 
    kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE" 
    echo 'Service stopped' >&2 
} 

uninstall() { 
    echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] " 
    local SURE 
    read SURE 
    if [ "$SURE" = "yes" ]; then 
    stop 
    rm -f "$PIDFILE" 
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2 
    update-rc.d -f <NAME> remove 
    rm -fv "$0" 
    fi 
} 

case "$1" in 
    start) 
    start 
    ;; 
    stop) 
    stop 
    ;; 
    uninstall) 
    uninstall 
    ;; 
    retart) 
    stop 
    start 
    ;; 
    *) 
    echo "Usage: $0 {start|stop|restart|uninstall}" 
esac 

複製到/etc/init.d:

cp "service.sh" "/etc/init.d/coolserver" 
chmod +x /etc/init.d/coolserver 

記得替換

<NAME> = coolserver 
<DESCRIPTION> = Describe your service here (be concise) 
<COMMAND> = /path/to/coolserver 
<USER> = Login of the system user the script should be run as 

啓動和測試您的服務和安裝服務在啓動時運行:

service coolserver start 
service coolserver stop 
update-rc.d coolserver defaults 
+0

好吧。這是問題,CentOS 6.8沒有systemd。 所以你的'systemctl'代碼將無法工作。 –

+0

現在@AliCoder怎麼樣? – CESCO

+0

是的就是這樣,我只是'update-rc.d coolserver defaults'給我'update-rc.d命令not found',還有一個問題我怎麼能把'--auth「這樣的參數user:password」 –

-1

我假設你試過使用apache web服務器。其實,Go Web服務器本身就夠了。主要目的是在系統服務中運行Go web服務器。因此,您可以使用tmuxhttps://tmux.github.io/nohup作爲系統服務運行。您也可以使用apache或nginx web服務器作爲代理。

+0

我說httpd爲例,當然Go網站服務器本身就夠了, 我需要的是爲centos 6創建一個服務文件來運行應用程序。 類似的東西https://github.com/jpillora/cloud-torrent/wiki/Installation但在centos 6中,因爲它沒有systemd。 –

相關問題