SystemdGoService

package module
v0.0.0-...-eb9fa34 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2019 License: MIT Imports: 8 Imported by: 0

README

Systemd Go Service

Create, edit, start, stop, enable and disable systemd services easily with Go.

Install

go get github.com/JojiiOfficial/SystemdGoService

Usage

main.go

package main

import (
	"github.com/JojiiOfficial/SystemdGoService"
)

func main() {
	service := SystemdGoService.NewDefaultService("testService", "this is a test", "/bin/sh /test.sh")
	service.Service.User = "root"
	service.Service.Group = "root"
	service.Create() 	// alternatively you can use service.Generate() to get the content as string instead
	service.Enable() 	// or service.Disable()
	service.Start() 	// or service.Stop()
	
	//You also can parse an existing service file
	service := SystemdGoService.Parse("sshd.service")
	service.Start()		// and start it
}

This creates following file (/etc/systemd/system/testService.service):

[Unit]
Description=this is a test
After=network.target

[Service]
Type=simple
ExecStart=/bin/sh /test.sh
User=root
Group=root

[Install]
WantedBy=multi-user.target

You can set

For [Unit]

  • Description
  • Documentation
  • Before
  • After
  • Wants
  • ConditionPathExisis
  • Conflicts

For [Service]

  • Type
  • ExecStartPre
  • ExecStart
  • ExecReload
  • ExecStop
  • RestartSec
  • User
  • Group
  • Restart
  • TimeoutStartSec
  • TimeoutStopSec
  • SuccessExitStatus
  • RestartPreventExitStatus
  • PIDFile
  • WorkingDirectory
  • RootDirectory
  • LogsDirectory
  • KillMode
  • ConditionPathExists
  • RemainAfterExit
  • EnvironmentFile
  • RuntimeDirectory
  • RuntimeDirectoryMode

For [Install]

  • WantedBy
  • Alias

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DaemonReload

func DaemonReload() error

DaemonReload reloads the daemon

func NameToServiceFile

func NameToServiceFile(name string) string

NameToServiceFile returns the name of the servicefile

func SetServiceStatus

func SetServiceStatus(service string, newStatus SystemdCommand) error

SetServiceStatus sets new status for service

func SystemfileExists

func SystemfileExists(name string) bool

SystemfileExists returns true if service exists

Types

type Install

type Install struct {
	WantedBy Target
	Alias    string
	Also     string
}

Install Install in .service file

type SService

type SService struct {
	Type                     ServiceType
	ExecStartPre             string
	ExecStart                string
	ExecReload               string
	ExecStop                 string
	RestartSec               string
	User                     string
	Group                    string
	Restart                  ServiceRestart
	TimeoutStartSec          int
	TimeoutStopSec           int
	SuccessExitStatus        string
	RestartPreventExitStatus string
	PIDFile                  string
	WorkingDirectory         string
	RootDirectory            string
	EnvironmentFile          string
	RuntimeDirectory         string
	RuntimeDirectoryMode     string
	LogsDirectory            string
	KillMode                 string
	ConditionPathExists      string
	RemainAfterExit          SystemdBool
}

SService Service in .service file

type Service

type Service struct {
	Name    string   `name:"name"`
	Unit    Unit     `name:"[Unit]"`
	Service SService `name:"[Service]"`
	Install Install  `name:"[Install]"`
}

Service service

func NewDefaultService

func NewDefaultService(name, description, execStart string) *Service

NewDefaultService creates a new default service

func NewService

func NewService(unit Unit, service SService, install Install) *Service

NewService creates a new service

func Parse

func Parse(fileName string) *Service

Parse a service file to a Service scruct

func (*Service) Create

func (service *Service) Create() error

Create creates a service file

func (*Service) Disable

func (service *Service) Disable() error

Disable disables a service

func (*Service) Enable

func (service *Service) Enable() error

Enable enables a service

func (*Service) Generate

func (service *Service) Generate() string

Generate generates a service to a .service file

func (*Service) Start

func (service *Service) Start() error

Start starts a service

func (*Service) Stop

func (service *Service) Stop() error

Stop stop

type ServiceRestart

type ServiceRestart string

ServiceRestart when the service should be restarted

const (
	//No don't restart
	No ServiceRestart = "no"
	//Always restart always
	Always ServiceRestart = "always"
	//OnSuccess restart only on success (exitcode=0 or on SIGHUP, SIGINT, SIGTERM or on SIGPIPE)
	OnSuccess ServiceRestart = "on-success"
	//OnFailure restart only on failure (exitcode != 0)
	OnFailure ServiceRestart = "on-failure"
	//OnAbnormal restart if the service was terminated by a signal, or an operation timed out
	OnAbnormal ServiceRestart = "on-abnormal"
	//OnAbort restart if the service was terminated by an non clean exit signal
	OnAbort ServiceRestart = "on-abort"
	//OnWatchdog restart if the watchdog timed out
	OnWatchdog = "on-watchdog"
)

type ServiceType

type ServiceType string

ServiceType type of service

const (
	//Simple simple service
	Simple ServiceType = "simple"
	//Notify tells the systemd if its initialzed
	Notify ServiceType = "notify"
	//Forking keep active if a fork is running but the parent has exited
	Forking ServiceType = "forking"
	//Dbus a dbus service
	Dbus ServiceType = "dbus"
	//Oneshot wait until the start action has finished until it consideres to be active
	Oneshot ServiceType = "oneshot"
	//Exec similar to simple
	Exec ServiceType = "exec"
)

type SystemdBool

type SystemdBool string

SystemdBool a bool (true=yes/false=no)

const (
	//True true
	True SystemdBool = "yes"
	//False false
	False SystemdBool = "no"
)

type SystemdCommand

type SystemdCommand int

SystemdCommand a command for systemd

const (
	//Stop stops a running service
	Stop SystemdCommand = 0
	//Start starts a stopped service
	Start SystemdCommand = 1
	//Enable enables a service to auto start
	Enable SystemdCommand = 2
	//Disable disables a service to auto start
	Disable SystemdCommand = 3
	//Restart restarts a service
	Restart SystemdCommand = 4
)

type Target

type Target string

Target systemd target

const (
	//NetworkTarget networktarget
	NetworkTarget Target = "network.target"
	//MultiuserTarget multiuser target
	MultiuserTarget Target = "multi-user.target"
	//SocketTarget socket target
	SocketTarget Target = "socket.target"
)

type Unit

type Unit struct {
	Description         string
	Documentation       string
	Before              Target
	After               Target
	Wants               Target
	ConditionPathExists string
	Conflicts           string
}

Unit Unit in .service file

Jump to

Keyboard shortcuts

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