goservice

package module
v0.0.0-...-6ec3d1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2023 License: Unlicense Imports: 9 Imported by: 0

README

go-service

Package to quickly implement linux services in go

why?

I've written this in one form or another a good number of times and I'd rather not continue doing so.

how to use

TODO

func serviceMain(cfgFilePath string, serviceLogger *log.Logger) {
    // do stuff
}

func main() {
    // let goservice handle the leg work
	goservice.Start(serviceMain)
}

works with systemd

example:

[Unit]
Description=Go Service Example
After=network.target

[Service]
Type=simple
User=some_service_user
Group=some_service_group
ExecStart=/path/to/my_exe -cfgfile=/path/to/my/config_file -syslog=true

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

Restart=on-failure
RestartPreventExitStatus=1

# Sets open_files_limit
LimitNOFILE = 10000

[Install]
WantedBy=multi-user.target
works with openrc too

TODO: verify this is 100% correct (it's probably not)

#!/sbin/openrc-run
  
name=$RC_SVCNAME
cfgfile="/path/to/my.conf"
pidfile="/run/$RC_SVCNAME/$RC_SVCNAME.pid"

command="/usr/bin/my_service"
command_args="--pidfile=$pidfile --cfgfile=$cfgfile"
command_user="service_user"
command_background="yes"

depend() {
        need net
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Start

func Start(serviceMain MainFunction)

daemon start, supply a go-service.MainFunction() in place of a normal main() function

parses startup arguments

  • pidfile=/path/to/pidfile if not supplied runs like a normal executable

  • cfgfile=/path/to/cfgfile this gets passed back to serviceMain for the user to config their service function also checks that file exists if a non-empty string is passed and throws a fatal error otherwise

Types

type MainFunction

type MainFunction func(string, *log.Logger)

Service main function

argument string is the path to a config file supplied in startup arguments argument logger is a log.Logger object, if run as a service logs to syslog

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL