I have service file, which starts and stops self-written proxy server.
[Unit]
Description=Golang proxy server in Docker
After=network.target docker.service
Requires=docker.service
[Service]
Type=simple
WorkingDirectory=/home/ooolledj/socks5-proxy
ExecStart=/usr/bin/docker container rm goproxy --force ; /usr/bin/docker run --name goproxy -p 12345:12345 goproxy:local
ExecStop=/usr/bin/docker container stop -t 1 goproxy
Restart=on-failure
TimeoutStartSec=10
[Install]
WantedBy=multi-user.target
It work fine, except case when i have to call systemctl stop goproxy
- container stops, with it service get "failed" state. After that, i can call systemctl start goproxy
and service is "active (running)" with no problem. I suppose it happens because container process (PID) dies, daemon see, that container process exited and raise error goproxy.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
.
root@my-test-host:/home/ooolledj/socks5-proxy# systemctl status goproxy
● goproxy.service - Golang proxy server in Docker
Loaded: loaded (/etc/systemd/system/goproxy.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2023-08-17 18:05:09 MSK; 881ms ago
Process: 49618 ExecStartPre=/usr/bin/docker container rm goproxy --force (code=exited, status=0/SUCCESS)
Process: 49624 ExecStart=/usr/bin/docker run --name goproxy -p 12345:12345 goproxy:local (code=exited, status=2)
Process: 49717 ExecStop=/usr/bin/docker container stop -t 3 goproxy (code=exited, status=0/SUCCESS)
Main PID: 49624 (code=exited, status=2)
CPU: 135ms
Aug 17 18:04:58 my-test-host systemd[1]: Starting Golang proxy server in Docker: https://github.com/OOOlledj/socks5-proxy...
Aug 17 18:04:58 my-test-host docker[49618]: goproxy
Aug 17 18:04:58 my-test-host systemd[1]: Started Golang proxy server in Docker: https://github.com/OOOlledj/socks5-proxy.
Aug 17 18:05:09 my-test-host systemd[1]: Stopping Golang proxy server in Docker: https://github.com/OOOlledj/socks5-proxy...
Aug 17 18:05:09 my-test-host docker[49717]: goproxy
Aug 17 18:05:09 my-test-host systemd[1]: goproxy.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Aug 17 18:05:09 my-test-host systemd[1]: goproxy.service: Failed with result 'exit-code'.
Aug 17 18:05:09 my-test-host systemd[1]: Stopped Golang proxy server in Docker: https://github.com/OOOlledj/socks5-proxy.
Can you suggest how to make service go to state "exited" on stop or else? It's just incorrect that service become failed - it did everything absolutely right.
I tried many options with ExecStop, ExecStart and ExecStartPre, but i have no positive result on it.
The solution was quite simple - add the code to main function to handle any signal with exit-code 0:
func main() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
_ = <-sigc
os.Exit(0)
}()
// working code below
...
}
Now my service stops with status like Active: inactive (dead)