2016-09-06 196 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。 –

相关问题