How do I get my Golang web server to run in the background?

前端 未结 5 961
我在风中等你
我在风中等你 2020-12-22 22:29

I have recently completed the Wiki web development tutorial (http://golang.org/doc/articles/wiki/). I had tons of fun and I would like to experiment more with the n

相关标签:
5条回答
  • 2020-12-22 23:07

    This will configure your service using systemd, not a comprehensive tutorial but rather a quick jump-start of how this can be set up.

    Content of your app.service file

    [Unit]  
    Description=deploy-webhook service
    After=network.target
    
    [Service]      
    ExecStart=/usr/bin/go webhook.go    
    WorkingDirectory=/etc/deploy-webhook
    
    User=app-svc      
    Group=app-svc
    
    Restart=always    
    RestartSec=10    
    KillSignal=SIGINT
    
    SyslogIdentifier=deploy-webhook-service      
    PrivateTmp=true  
    
    Environment=APP_PARAM_1=ParamA
    Environment=APP_PARAM_2=ParamB
    
    [Install]      
    WantedBy=multi-user.target  
    

    Starting the Service

    sudo systemctl start deploy-webhook.service
    

    Service Status

    sudo systemctl status deploy-webhook.service
    

    Logs

    journalctl -u deploy-webhook -e
    
    0 讨论(0)
  • 2020-12-22 23:08

    Simple / Usable things first

    If you want a start script without much effort, you could use the upstart service. See the corresponding manual page and /etc/init/*.conf for examples. After creating such a process you can start your server by calling

    service myserver start
    

    If you want more features, like specific limitations or permission management, you could try xinetd.

    Using the shell

    You could start your process like this:

    nohup ./myexecutable &
    

    The & tells the shell to start the command in the background, keeping it in the job list. On some shells, the job is killed if the parent shell exits using the HANGUP signal. To prevent this, you can launch your command using the nohup command, which discards the HANGUP signal.

    However, this does not work, if the called process reconnects the HANGUP signal.

    To be really sure, you need to remove the process from the shell's joblist. For two well known shells this can be achieved as follows:

    bash:

    ./myexecutable &
    disown <pid>
    

    zsh:

    ./myexecutable &!
    

    Killing your background job

    Normally, the shell prints the PID of the process, which then can be killed using the kill command, to stop the server. If your shell does not print the PID, you can get it using

    echo $!
    

    directly after execution. This prints the PID of the forked process.

    0 讨论(0)
  • 2020-12-22 23:12

    You could use Supervisord to manage your process.

    0 讨论(0)
  • 2020-12-22 23:12

    After you press ctrl+z (putting the current task to sleep) you can run the command bg in the terminal (stands for background) to let the latest task continue running in the background.

    When you need to, run fg to get back to the task.

    To get the same result, you can add to your command & at the end to start it in the background.

    0 讨论(0)
  • 2020-12-22 23:31

    Ubuntu? Use upstart.

    Create a file in /etc/init for your job, named your-service-name.conf

    start on net-device-up
    exec /path/to/file --option
    

    You can use start your-service-name, as well as: stop, restart, status

    0 讨论(0)
提交回复
热议问题