Daemonize golang options and method

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.

[Unit]
Description=Comm Test
ConditionPathExists=/usr/bin/commTest
After=network.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/usr/bin/commTest
ExecStart=/usr/lib/golang/bin run .
Restart=on-failure
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=commTest
[Install]
WantedBy=multi-user.target

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[1]: commTest.service: main process exited, code=exited, status=203/EXEC
Jan 03 20:48:06 localhost.localdomain systemd[1]: Unit commTest.service entered failed state.
Jan 03 20:48:06 localhost.localdomain systemd[1]: 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:
WorkingDirectory=/usr/lib/golang/
ExecStart=/usr/lib/golang/go_executable

and omit run .

Here is how I set systemd:

[Unit]
Description=gowebdev

[Service]
Type=simple
User=root
Restart=always
RestartSec=5s
WorkingDirectory=/var/www/gowebdev
ExecStart=/var/www/gowebdev/main

[Install]
WantedBy=multi-user.target

EDIT Also check that the Go executable have chmod 0755

Hi Sibert,
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!

Hi Sibert,
Thank you about webmin let me explore and study more about it. If I am not sure might get confirmation from yourself.

Hi Nobbz,
Thank you will study more on this permission and user in linux.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.