DEV Community

Masui Masanori
Masui Masanori

Posted on

[Xubuntu] Automatic start-up Go applications and Nginx

Intro

This time, I will try automatic starting-up my Go application and Nginx.

I built my Go application by "go build .".

To start-up automatically, I will add systemd service files.

Go applications

I add a service file into "/etc/systemd/system/".

webappsample.service

[Service]
Type=simple
User=root
ExecStart=/home/example/go/src/sample/webappsample/webappsample
WorkingDirectory=/home/example/go/src/sample/webappsample
Restart=always
RestartSec=30

[Install]
WantedBy = multi-user.target
Enter fullscreen mode Exit fullscreen mode

After adding the file, I register it with systemd.

sudo systemctl enable webappsample.service
Enter fullscreen mode Exit fullscreen mode

Nginx

Nginx service files are almost identical to Go application.

nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Top comments (0)