systemd

package
v0.0.0-...-003a09f Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SystemdMicrosecond = 1
	SystemdMillisecond = 1000
	SystemdSecond      = 1000000
	SystemdMinute      = 60000000
	SystemdHour        = 3600000000
	SystemdDay         = 86400000000
	SystemdWeek        = 604800000000
	SystemdMonth       = 2629800000000
	SystemdYear        = 31557600000000
)

Constants of systemd.time(7)#PARSING TIME SPANS time units.

Variables

This section is empty.

Functions

func GetAllUnitNames

func GetAllUnitNames(u []Unit) []string

Gets all unit names from a slice of units.

func GetEnabledTimersDir

func GetEnabledTimersDir(unitdir string) string

Returns the directory where to look for unit files that are enabled in the specified unit directory.

Returns the unit dir with the sub directory "/timers.target.wants".

func GetUnitNameFromFilename

func GetUnitNameFromFilename(filename string) string

Gets the unit name from a filename of .timer- and .service-files.

func ListFilesFromDirWithExtension

func ListFilesFromDirWithExtension(dir string, extension string) ([]string, error)

Lists all files in the specific directory with a specific extension.

func ListFilesFromDirsWithExtension

func ListFilesFromDirsWithExtension(dirs []string, extension string) ([]string, error)

Lists all files in the specific directories with a specific extension.

func NextUnit

func NextUnit(all []int, def int) (int, bool)

Gives the next number of a slice of numbers (All) after a number (Default)

If no numbers are preferred (All empty) Default is returned

Returned is the next number, and weather the next unit will be overfloused, because the next number is smaller than the Default

Used to find the next number of a unit.

func NextUnitAfter

func NextUnitAfter(all []int, after int) int

Gives the next number of a slice of numbers (All) after a number (After) If no numbers are preferred (All empty) After + 1 is returned Used to overflow a number.

func NextUnitWithoutDefault

func NextUnitWithoutDefault(all []int) int

Gives the next number of a slice of numbers (All) without a default number

Used if the next number is known to overflow and therefore doesn't need a default.

func ParseChainedNumbers

func ParseChainedNumbers(chained string) ([]int, error)

Returns real numbers from systemd's number chaining, like "1,3,5", "1..3", "1,3..5" or "*", to []int{1, 3, 5}, []int{1, 2, 3, []int{1, 3, 4, 5}, []int{} respectively.

func ParseSingleSplittedTimeSpan

func ParseSingleSplittedTimeSpan(digits int, unit string) (int, error)

Parses a single time span unit into a time.Duration defined by systemd.time(7)#PARSING TIME SPANS.

func ParseSystemdBool

func ParseSystemdBool(text string) (bool, error)

Parses a boolean string into a boolean defined by systemd.syntax(7)#INTRODUCTION.

func ParseTimeSpan

func ParseTimeSpan(text string) (time.Duration, error)

Parses a time span string into a time.Duration defined by systemd.time(7)#PARSING TIME SPANS.

func ParseWeekday

func ParseWeekday(weekday string) (time.Weekday, error)

Returns time.Weekday from systemd's weekdays definition.

func ParseWeekdays

func ParseWeekdays(weekdays string) ([]time.Weekday, error)

Returns time.Weekday from systemd's weekday chaining.

Types

type CalendarEvent

type CalendarEvent struct {
	// The locations (timezone) in which the times of this object (below) are defined
	Location time.Location

	// Weekdays that the event should occur
	Weekdays []time.Weekday
	// Years that the event should occur
	Years []int
	// Months that the event should occur
	Months []time.Month
	// Days of the month, that the event should occur
	Days []int
	// Hours of the day, that the event should occur
	Hours []int
	// Minutes of the hour, that the event should occur
	Minutes []int
	// Seconds of the minute, that the event should occur
	Seconds []int

	// TODO: what is that?
	Empty bool
}

Calendar event specified by systemd.time(7)#CALENDAR EVENTS

Calendar events may be used to refer to one or more points in time in a single expression.

Empty array means all values aka. no preference.

func NewCalendarEvent

func NewCalendarEvent(calendar string) (CalendarEvent, error)

NewCalendarEvent creates a new CalendarEvent from a calendar string.

func (CalendarEvent) Next

func (event CalendarEvent) Next(now time.Time) (time.Time, error)

Like NextWithoutWeekdays but additionally also checks weekdays.

func (CalendarEvent) NextWithoutWeekdays

func (event CalendarEvent) NextWithoutWeekdays(now time.Time) time.Time

NextWithoutWeekdays returns the next time of the event without considering weekdays

First it sets the units without considering other units Then if a overflow is found, it overflows the next unit and resets the previous units.

func (CalendarEvent) Normalize

func (event CalendarEvent) Normalize() string

Normalize returns the calendar event in a normalized format.

type IniFile

type IniFile struct {
	// All sections of the INI file.
	Sections map[string][]IniKeyPair
}

Content of a systemd INI configuration file (like .service or .timer files).

func NewIniFile

func NewIniFile(file *os.File) (IniFile, error)

Reads a systemd INI configuration file and returns an IniFile object.

func (IniFile) GetValue

func (inifile IniFile) GetValue(section string, key string, defaultvalue string) string

Returns the value of a key in a section of an INI file, or a default value if the key is not found.

systemd's configuration format supports multiple values for a single key. If you need all values of a key, use GetValues() instead.

Often, multiple settings form a list, and setting to an empty value "resets", which means that previous assignments are ignored. If the key has multiple values, only the last value is returned.

func (IniFile) GetValues

func (inifile IniFile) GetValues(section string, key string, defaultvalues []string) []string

Returns the value(s) of a key in a section of an INI file, or a default value(s) if the key is not found.

systemd's configuration format supports multiple values for a single key, so this function returns a slice of strings. If the key only supports a single value, use GetValue() instead.

type IniKeyPair

type IniKeyPair struct {
	// The key of the key-value pair.
	Key string
	// The value of the key-value pair.
	Value string
}

A single key-value pair of an INI file.

type Paths

type Paths struct {
	// Directory where to store stamp files
	// Stamps are used to store the last time a timer was executed
	StampDir string
	// Directories where to look for unit files (.service, .timer)
	UnitDirs []string
}

Configuration where to look for unit directories and stamp files.

func NewSystemPaths

func NewSystemPaths() Paths

Creates a new Paths object with default values for starting in system-mode.

func (*Paths) AddSystemSystemdPaths

func (paths *Paths) AddSystemSystemdPaths()

Adds the default systemd unit directories in system-mode.

func (*Paths) AddUnitDir

func (paths *Paths) AddUnitDir(dir string)

Adds a custom unit directory.

func (Paths) GetEnabledTimersDirs

func (paths Paths) GetEnabledTimersDirs() []string

Returns all directories where to look for unit files that are enabled.

Returns each unit dir with the sub directory "/timers.target.wants".

type Service

type Service struct {
	// "Unit" section of the service.
	UnitInfo

	// Path to the .service file.
	Path string
	// The name of the unit.
	// This is the name of the file without the extension.
	UnitName string

	// Options of the service:
	// Defined in systemd.service(5)#OPTIONS.
	Type      string
	ExecStart []string
}

A systemd.service(5) definition.

func NewService

func NewService(path string) (Service, error)

Loads a systemd service file and returns a Service struct.

func (Service) Start

func (service Service) Start()

Starts a service.

type Timer

type Timer struct {
	// "Unit" section of a .timer file.
	UnitInfo

	// Path to the .timer file.
	Path string
	// The filename of the file
	Filename string
	// The name of the unit.
	// This is the name of the file without the extension.
	Name string
	// Paths configured by the application
	Paths Paths
	// The unit directory where the file is located
	Dir string

	// The next time the timer will run
	Next time.Time
	// Weather this timer is enable to start on daemon startup.
	Enabled bool

	// Options of the timer:
	// Defined in systemd.timer(5)#OPTIONS.
	OnBootSec          time.Duration
	OnCalendar         CalendarEvent
	AccuracySec        time.Duration
	RandomizedDelaySec time.Duration
	Persistent         bool
	UnitName           string
}

A systemd.timer(5) definition.

func GetEnabledTimers

func GetEnabledTimers(paths Paths) ([]Timer, error)

Gets all enabled timers in Paths.

func GetTimers

func GetTimers(paths Paths) ([]Timer, error)

Gets all timers in Paths.

func NewTimer

func NewTimer(path string, now time.Time, paths Paths) (Timer, error)

Loads a systemd timer file and returns a Timer struct. From systemd.timer(5)#OPTIONS.

func (Timer) Disable

func (timer Timer) Disable()

Disables a timer from starting on daemon startup, by deleting every symlink in every enabled timers directories of Paths.

func (Timer) Enable

func (timer Timer) Enable(unitDir string) error

Enables a timer to start on daemon startup, by creating a symlink in the enabled timers directory (either on unitDir or if unitDir is "", then the Unit dir of the timer).

func (Timer) GetEnabled

func (timer Timer) GetEnabled() (bool, error)

Returns weather this timer is enabled to start on daemon startup.

func (Timer) GetLastRun

func (timer Timer) GetLastRun() (time.Time, error)

Provides the time of the last run of a timer.

func (Timer) NextSince

func (timer Timer) NextSince(since time.Time) (time.Time, error)

Returns the time of the next trigger of a timer since time.

func (*Timer) SetLastRun

func (timer *Timer) SetLastRun(t time.Time) error

Sets the time of the last run of a timer.

func (Timer) TriggerTimeOfBootSec

func (timer Timer) TriggerTimeOfBootSec() (time.Time, error)

Returns the time of the trigger of a timer based on BootSec.

type Unit

type Unit struct {
	Timer   Timer
	Service Service

	Name string
}

A systemd unit with a timer and a service.

Note that there may be multiple Unit structs for a single systemd unit, because each Unit object always holds a single Timer'.

func GetUnits

func GetUnits(paths Paths) ([]Unit, error)

Gets all units with timers in Paths.

It checks all .timer files in the unit directories, and then finds the corresponding .service file.

func GetUnitsFromTimers

func GetUnitsFromTimers(timers []Timer, paths Paths) ([]Unit, error)

Gets for each timer their specific Unit (with .service files).

func GetUnitsWithEnabledTimers

func GetUnitsWithEnabledTimers(paths Paths) ([]Unit, error)

Gets all units in Paths, that have a timer enabled.

type UnitInfo

type UnitInfo struct {
	Description string
}

"Unit" section of a systemd files (.timer, .service). From systemd.unit(5)#[UNIT] SECTION OPTIONS.

func NewUnitInfo

func NewUnitInfo(inifile IniFile) UnitInfo

Creates a UnitInfo struct from an IniFile. From systemd.unit(5)#[UNIT] SECTION OPTIONS.

Jump to

Keyboard shortcuts

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