4

So I wrote a golang app which I want initialized by systemd whenever my server gets a reboot.

Here's the service file:-

[Unit]
Description=golang wiki initialization

[Service]
Type=forking
PIDFile=/tmp/gowiki.pid-3030
User=root
Group=root
WorkingDirectory=/var/www
ExecStart=/bin/bash -c 'daemonize -o stdout.log -e stderr.log /var/www/wiki'

[Install]
WantedBy=multi-user.target

It appears to work well whenever I reboot the server but I am still running into slightly irritating problem when I am manually starting or restarting gowiki like this in terminal:-

systemctl start gowiki

or

systemctl restart gowiki

This does get my gowiki process running; however, after I execute my systemctl start gowiki command, I am frozen in my terminal and the command does not seem to exit until I manually force a Ctrl-C.

What am I doing wrong to be causing this problem?

7

This worked.

[Unit]
Description=golang wiki initialization

[Service]
PIDFile=/tmp/gowiki.pid-4040
User=root
Group=root
WorkingDirectory=/var/www
ExecStart=/bin/bash -c '/var/www/wiki'

[Install]
WantedBy=multi-user.target

Because the Type=forking services expect the parent service to exit before the service has actually started. If the parent never exits, then systemctl start will not give you back a prompt because it doesn't think that it's done executing.

0

If you don't mind adjusting your code a little for systemd the cleanest solution I've found thus far is using the SDNotify-signal.

import "github.com/coreos/go-systemd/daemon"

func main() {
    // Important init stuff here
    ....

    // Notify systemd that we're ready
    sent, e := daemon.SdNotify(false, "READY=1")
    if e != nil {
        log.Fatal(e)
    }
    if !sent {
        log.Printf("SystemD notify NOT sent\n")
    }

    // And do something blocking like listening for HTTP(s)
  • It has no CGO-deps
  • The code also works on non-systemd systems (in this case it emits a warning notify not sent)
  • If you want a working example: https://github.com/mpdroog/dnsleak

Your corresponding systemd unit-file:

[Unit]
Description=golang wiki initialization
After=network.target
Requires=network.target

[Service]
Type=notify

Restart=always
RestartSec=30
TimeoutStartSec=0

WorkingDirectory=/var/www
ExecStart=/var/www/wiki
User=www-data
Group=www-data
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.