Class: Botiasloop::SystemdService

Inherits:
Object
  • Object
show all
Defined in:
lib/botiasloop/systemd_service.rb

Overview

Systemd user service management for auto-start on boot

Manages installation, enablement, and control of botiasloop as a systemd user service. This allows botiasloop to start automatically on user login and run in the background.

Examples:

Install and enable the service

service = Botiasloop::SystemdService.new
service.install
service.enable
service.start

Check status

service = Botiasloop::SystemdService.new
status = service.status
puts "Running: #{status[:active]}"

Constant Summary collapse

SERVICE_NAME =

Service name used by systemd

"botiasloop.service"

Instance Method Summary collapse

Constructor Details

#initializeSystemdService

Initialize a new SystemdService instance



28
29
# File 'lib/botiasloop/systemd_service.rb', line 28

def initialize
end

Instance Method Details

#active?Boolean

Check if the service is currently active/running

Returns:

  • (Boolean)

    True if service is active



57
58
59
60
61
# File 'lib/botiasloop/systemd_service.rb', line 57

def active?
  return false unless systemd_available?

  systemctl_quiet("is-active", SERVICE_NAME)
end

#disableBoolean

Disable the service from starting on boot

Disables the service then disables linger if it was enabled.

Returns:

  • (Boolean)

    True on success

Raises:



117
118
119
120
121
122
123
# File 'lib/botiasloop/systemd_service.rb', line 117

def disable
  raise SystemdError, "systemd is not available on this system" unless systemd_available?

  systemctl("disable", SERVICE_NAME)
  disable_linger
  true
end

#disable_lingerBoolean

Disable linger for the current user

Prevents user services from starting at boot time. Does nothing if already disabled.

Returns:

  • (Boolean)

    True on success or already disabled, false on failure



154
155
156
157
158
# File 'lib/botiasloop/systemd_service.rb', line 154

def disable_linger
  return true unless linger_enabled?

  system("loginctl", "disable-linger", ENV["USER"])
end

#enableBoolean

Enable the service to start on boot

Enables linger to allow user services to start at boot time, then enables the service. Falls back gracefully if linger fails.

Returns:

  • (Boolean)

    True on success

Raises:

  • (SystemdError)

    If systemd unavailable or service not installed



102
103
104
105
106
107
108
109
# File 'lib/botiasloop/systemd_service.rb', line 102

def enable
  raise SystemdError, "systemd is not available on this system" unless systemd_available?
  raise SystemdError, "Service is not installed" unless installed?

  enable_linger
  systemctl("enable", SERVICE_NAME)
  true
end

#enable_lingerBoolean

Enable linger for the current user

Allows user services to start at boot time. Does nothing if already enabled.

Returns:

  • (Boolean)

    True on success or already enabled, false on failure



142
143
144
145
146
# File 'lib/botiasloop/systemd_service.rb', line 142

def enable_linger
  return true if linger_enabled?

  system("loginctl", "enable-linger", ENV["USER"])
end

#enabled?Boolean

Check if the service is enabled to start on boot

Returns:

  • (Boolean)

    True if service is enabled



48
49
50
51
52
# File 'lib/botiasloop/systemd_service.rb', line 48

def enabled?
  return false unless systemd_available?

  systemctl_quiet("is-enabled", SERVICE_NAME)
end

#installBoolean

Install the service file

Creates the systemd user directory if needed and writes the service configuration file.

Returns:

  • (Boolean)

    True on success

Raises:



70
71
72
73
74
75
76
77
# File 'lib/botiasloop/systemd_service.rb', line 70

def install
  FileUtils.mkdir_p(systemd_user_dir)
  File.write(service_file_path, service_template)
  systemctl("daemon-reload")
  true
rescue => e
  raise SystemdError, "Failed to install service: #{e.message}"
end

#installed?Boolean

Check if the service file is installed

Returns:

  • (Boolean)

    True if service file exists



41
42
43
# File 'lib/botiasloop/systemd_service.rb', line 41

def installed?
  File.exist?(service_file_path)
end

#linger_enabled?Boolean

Check if linger is enabled for the current user

Linger allows user services to start at boot time without requiring a user login session.

Returns:

  • (Boolean)

    True if linger is enabled



131
132
133
134
# File 'lib/botiasloop/systemd_service.rb', line 131

def linger_enabled?
  output = `loginctl show-user $USER --property=Linger 2>/dev/null`.strip
  output == "Linger=yes"
end

#logs(follow: false, lines: 50) ⇒ Boolean

Display service logs using journalctl

Parameters:

  • follow (Boolean) (defaults to: false)

    Whether to follow logs in real-time (tail -f mode)

  • lines (Integer) (defaults to: 50)

    Number of lines to show (default: 50)

Returns:

  • (Boolean)

    True on success

Raises:



213
214
215
216
217
218
219
220
# File 'lib/botiasloop/systemd_service.rb', line 213

def logs(follow: false, lines: 50)
  raise SystemdError, "systemd is not available on this system" unless systemd_available?

  args = ["--user", "-u", SERVICE_NAME, "-n", lines.to_s]
  args << (follow ? "-f" : "--no-pager")

  system("journalctl", *args)
end

#restartBoolean

Restart the service

Returns:

  • (Boolean)

    True on success

Raises:

  • (SystemdError)

    If systemd unavailable or service not installed



187
188
189
190
191
192
193
# File 'lib/botiasloop/systemd_service.rb', line 187

def restart
  raise SystemdError, "systemd is not available on this system" unless systemd_available?
  raise SystemdError, "Service is not installed" unless installed?

  systemctl("restart", SERVICE_NAME)
  true
end

#startBoolean

Start the service

Returns:

  • (Boolean)

    True on success

Raises:

  • (SystemdError)

    If systemd unavailable or service not installed



164
165
166
167
168
169
170
# File 'lib/botiasloop/systemd_service.rb', line 164

def start
  raise SystemdError, "systemd is not available on this system" unless systemd_available?
  raise SystemdError, "Service is not installed" unless installed?

  systemctl("start", SERVICE_NAME)
  true
end

#statusHash

Get service status information

Returns:

  • (Hash)

    Status with :installed, :enabled, :active, :message keys



198
199
200
201
202
203
204
205
# File 'lib/botiasloop/systemd_service.rb', line 198

def status
  {
    installed: installed?,
    enabled: enabled?,
    active: active?,
    message: status_message
  }
end

#stopBoolean

Stop the service

Returns:

  • (Boolean)

    True on success

Raises:



176
177
178
179
180
181
# File 'lib/botiasloop/systemd_service.rb', line 176

def stop
  raise SystemdError, "systemd is not available on this system" unless systemd_available?

  systemctl("stop", SERVICE_NAME)
  true
end

#systemd_available?Boolean

Check if systemd is available on the system

Returns:

  • (Boolean)

    True if systemctl is available



34
35
36
# File 'lib/botiasloop/systemd_service.rb', line 34

def systemd_available?
  !`which systemctl 2>/dev/null`.strip.empty?
end

#uninstallBoolean

Uninstall the service

Stops the service if running, disables it, removes the service file, and reloads systemd.

Returns:

  • (Boolean)

    True if uninstalled, false if not installed



85
86
87
88
89
90
91
92
93
# File 'lib/botiasloop/systemd_service.rb', line 85

def uninstall
  return false unless installed?

  stop if active?
  disable if enabled?
  FileUtils.rm_f(service_file_path)
  systemctl("daemon-reload")
  true
end