doudan4834
2016-09-06 12:25
浏览 50

通过服务运行Go应用

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.
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.

图片转代码服务由CSDN问答提供 功能建议

在CentOS 6.8中,我有一个golang应用程序,该应用程序在命令 go run main.go ,并且我需要创建一个系统服务来像服务httpd一样在启动时运行它。
我知道我必须创建 /etc/rc.d/init.d/httpd 之类的文件 但是我不知道如何执行该命令。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanrang2627 2016-09-08 20:19
    已采纳

    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:          <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
    

    Copy to /etc/init.d:

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

    Remember to replace

    <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 
    

    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
    
    点赞 打赏 评论
  • dongtuoji5396 2016-09-07 07:04

    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.

    点赞 打赏 评论

相关推荐 更多相似问题