Class: Botiasloop::SystemdService
- Inherits:
-
Object
- Object
- Botiasloop::SystemdService
- 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.
Constant Summary collapse
- SERVICE_NAME =
Service name used by systemd
"botiasloop.service"
Instance Method Summary collapse
-
#active? ⇒ Boolean
Check if the service is currently active/running.
-
#disable ⇒ Boolean
Disable the service from starting on boot.
-
#disable_linger ⇒ Boolean
Disable linger for the current user.
-
#enable ⇒ Boolean
Enable the service to start on boot.
-
#enable_linger ⇒ Boolean
Enable linger for the current user.
-
#enabled? ⇒ Boolean
Check if the service is enabled to start on boot.
-
#initialize ⇒ SystemdService
constructor
Initialize a new SystemdService instance.
-
#install ⇒ Boolean
Install the service file.
-
#installed? ⇒ Boolean
Check if the service file is installed.
-
#linger_enabled? ⇒ Boolean
Check if linger is enabled for the current user.
-
#logs(follow: false, lines: 50) ⇒ Boolean
Display service logs using journalctl.
-
#restart ⇒ Boolean
Restart the service.
-
#start ⇒ Boolean
Start the service.
-
#status ⇒ Hash
Get service status information.
-
#stop ⇒ Boolean
Stop the service.
-
#systemd_available? ⇒ Boolean
Check if systemd is available on the system.
-
#uninstall ⇒ Boolean
Uninstall the service.
Constructor Details
#initialize ⇒ SystemdService
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
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 |
#disable ⇒ Boolean
Disable the service from starting on boot
Disables the service then disables linger if it was enabled.
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_linger ⇒ Boolean
Disable linger for the current user
Prevents user services from starting at boot time. Does nothing if already disabled.
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 |
#enable ⇒ Boolean
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.
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_linger ⇒ Boolean
Enable linger for the current user
Allows user services to start at boot time. Does nothing if already enabled.
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
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 |
#install ⇒ Boolean
Install the service file
Creates the systemd user directory if needed and writes the service configuration file.
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
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.
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
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 |
#restart ⇒ Boolean
Restart the service
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 |
#start ⇒ Boolean
Start the service
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 |
#status ⇒ Hash
Get service status information
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: } end |
#stop ⇒ Boolean
Stop the service
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
34 35 36 |
# File 'lib/botiasloop/systemd_service.rb', line 34 def systemd_available? !`which systemctl 2>/dev/null`.strip.empty? end |
#uninstall ⇒ Boolean
Uninstall the service
Stops the service if running, disables it, removes the service file, and reloads systemd.
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 |