On this page, I will post a simple HTTP server code in Go, and systemd will automatically start the program of the Linux (Ubuntu, Archlinux) service.
$ pwd
$HOME/go/src/SimpleHttp
$ cat SimpleHttp.go
package main
import (
"fmt"
"net/http"
)
func SimpleHttpHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Simple HTTP server&Installation of Systemd Service")
}
func main() {
http.HandleFunc("/", SimpleHttpHandler)
//Start on port 8080
http.ListenAndServe(":8080", nil)
}
$ pwd
$HOME/go/src/SimpleHttp
$ tree
.
└── SimpleHttp.go
0 directories, 1 file
$ go build SimpleHttp.go
$ tree
.
├── SimpleHttp
└── SimpleHttp.go
0 directories, 2 files
$ cat /lib/systemd/system/SimpleHttp.service
[Unit]
Description = SimpleHttp.service daemon
[Service]
Environment="GOPATH=$HOME/go"
ExecStart=$HOME/go/src/SimpleHttp/SimpleHttp
Restart=always
Type=simple
User=ubuntu
Group=ubuntu
# Other directives omitted
# (file size)
LimitFSIZE=infinity
# (cpu time)
LimitCPU=infinity
# (virtual memory size)
LimitAS=infinity
# (open files)
LimitNOFILE=64000
# (processes/threads)
LimitNPROC=64000
[Install]
WantedBy = multi-user.target
Always run the following command after creating or modifying a new unit file, SimpleHttp.service.
sudo systemctl daemon-reload
$ sudo systemctl enable SimpleHttp.service
$ sudo systemctl start SimpleHttp.service
sudo systemctl stop SimpleHttp.service
$ sudo systemctl disable SimpleHttp.service
$ uname -a
Linux arch.local 5.6.10-arch1-1 #1 SMP PREEMPT Sat, 02 May 2020 19:11:54 +0000 x86_64 GNU/Linux
$ lsb_release -a
LSB Version: 1.4
Distributor ID: Arch
Description: Arch Linux
Release: rolling
Codename: n/a
$ uname -a
Linux ip-172-31-8-186 5.3.0-1017-aws #18~18.04.1-Ubuntu SMP Wed Apr 8 15:12:16 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
Recommended Posts