通过服务运行Go应用程序

huangapple go评论76阅读模式
英文:

Run go app by service

问题

在CentOS 6.8中,我有一个使用golang编写的应用程序,通过命令go run main.go运行,我需要创建一个系统服务,在启动时像service httpd一样运行它。

我知道我需要创建一个名为/etc/rc.d/init.d/httpd的文件,但我不知道如何编写它以运行该命令。

英文:

In CentOS 6.8 I have a golang app , that run in command go run main.go and I need to create a system service to run it in boot like service httpd.<br>
I know that I have to create file like /etc/rc.d/init.d/httpd But I don't know how to do it to run that command.

答案1

得分: 2

首先,您需要构建您的Go二进制文件并将其放在您的路径中。

go install main.go

如果您的“main”文件名为main,则go install将在您的路径中放置一个名为“main”的二进制文件,因此建议您将文件重命名为您称呼您的项目/服务器的名称。

mv main.go coolserver.go
go install coolserver.go

您可以运行coolserver以确保一切正常。如果您正确设置了$GOPATH,它应该可以正常运行。

这是一个名为service.sh的init.d服务的示例:

#!/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
    ;;
  restart)
    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> = 在此处描述您的服务(简洁明了)
<COMMAND> = /path/to/coolserver
<USER> = 脚本应作为其运行的系统用户的登录名

启动和测试您的服务,并将服务安装为在启动时运行:

service coolserver start
service coolserver stop
update-rc.d coolserver defaults
英文:

First, you will need to build your Go binary and put it in your path.

go install main.go

If your "main" file is called main, go install will place a binary called "main" in your path, so I suggest you rename your file to whatever you call your project/server.

mv main.go coolserver.go
go install coolserver.go

You can run coolserver to make sure everything is fine. It will if you have your $GOPATH setup properly.

Here it is an example of a init.d service called service.sh

#!/bin/sh
### BEGIN INIT INFO
# Provides:          &lt;NAME&gt;
# 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:       &lt;DESCRIPTION&gt;
### END INIT INFO

SCRIPT=&lt;COMMAND&gt;
FLAGS=&quot;--auth=user:password&quot;
RUNAS=&lt;USERNAME&gt;

PIDFILE=/var/run/&lt;NAME&gt;.pid
LOGFILE=/var/log/&lt;NAME&gt;.log

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

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

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

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

Copy to /etc/init.d:

cp &quot;service.sh&quot; &quot;/etc/init.d/coolserver&quot;
chmod +x /etc/init.d/coolserver

Remember to replace

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

Start and test your service and install the service to be run at boot-time:

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

答案2

得分: -2

我假设你尝试使用Apache Web服务器。实际上,Go Web服务器本身就足够了。主要目的是将Go Web服务器运行为系统服务。因此,你可以使用tmux(https://tmux.github.io/)或nohup来作为系统服务运行。你也可以使用Apache或Nginx Web服务器作为代理。

英文:

I assume you tried to use apache web server. Actually, Go web server is enough itself. Main purpose is to run Go web server in system service.So, you can use tmux https://tmux.github.io/ or nohup to run as system service. You can also use apache or nginx web server as proxy.

huangapple
  • 本文由 发表于 2016年9月6日 20:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/39348993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定