I have created a simple socket server via golang. So I have build then run it via ./commTest and it works fine. I have google and found it here GoLang: Running a Go binary as a systemd service on Ubuntu 18.04 in 10 Minutes (without Docker) | by Luci Bro | Medium and followed it accordingly and change what is necessary.
ExecStart=/usr/lib/golang/bin run .
I have disable the selinux via setenforce 0. Then when I run the status I get this error message.
Redirecting to /bin/systemctl status -l commTest.service
● commTest.service - Comm Test
Loaded: loaded (/etc/systemd/system/commTest.service; enabled; vendor preset: disabled)
Active: activating (auto-restart) (Result: exit-code) since Tue 2023-01-03 20:48:06 +08; 7s ago
Process: 48515 ExecStart=/usr/lib/golang/bin run . (code=exited, status=203/EXEC)
Main PID: 48515 (code=exited, status=203/EXEC)
Jan 03 20:48:06 localhost.localdomain systemd: commTest.service: main process exited, code=exited, status=203/EXEC
Jan 03 20:48:06 localhost.localdomain systemd: Unit commTest.service entered failed state.
Jan 03 20:48:06 localhost.localdomain systemd: commTest.service failed.
What I would like to find out is that besides this method is there any other method to run golang as daemon or this is the best method ?
I have managed to run it ready by changing this line ExecStart=/usr/lib/golang/bin run . to /usr/bin/commTest/commTest. But anyway is this the right method to daemonise or any other better method for golang ?
There is a mismatch between path and execute.
The path should be almost equal in both:
and omit run .
Here is how I set systemd:
EDIT Also check that the Go executable have chmod 0755
Thank you yes I put my solution above after I found my mistake too. Few more questions I would like to confirm with you how about the User=root must it be root ? Also best how to maintain the logs. Lastly for golang will you recommend this method or any other method ?
I think so. Root is the “manager” of Linux.
I use Webmin to manage both VPS (Debian) and to view all logs.
Systemd is to me the standard way to start/restart/shutdown services.
Webmin have the possibility to create shortcuts for service management. And to easy manage several systemd tasks.
Totally depends, but as a rule of thumb:
It can be any user if you know about permissions and capabilities and how to drop them.
If you don’t know, it really shouldn’t be root, so learn about how Linux manages them!
Thank you about webmin let me explore and study more about it. If I am not sure might get confirmation from yourself.
Thank you will study more on this permission and user in linux.