pub struct SystemdService { /* private fields */ }Expand description
Manages a systemd service based on a ServiceConfig.
This struct provides methods to generate the service file content,
write it to disk, and interact with systemctl to manage the service.
Implementations§
Source§impl SystemdService
impl SystemdService
Sourcepub fn new(config: ServiceConfig) -> Self
pub fn new(config: ServiceConfig) -> Self
Creates a new SystemdService from a given configuration.
Sourcepub fn write(&self, path: &Path) -> Result<(), Error>
pub fn write(&self, path: &Path) -> Result<(), Error>
Writes the generated service file content to the specified path.
§Errors
Error::Permissionif not run with root privileges.Error::Ioon file creation or write failures.
Sourcepub fn install_and_enable(&self) -> Result<(), Error>
pub fn install_and_enable(&self) -> Result<(), Error>
Installs, enables, and reloads the systemd daemon.
This is the primary method for setting up a new service. It performs:
- Writes the service file to
/etc/systemd/system/. - Reloads the systemd daemon (
systemctl daemon-reload). - Enables the service (
systemctl enable).
§Errors
Error::Permissionif not run with root privileges.Error::Ioif the service file already exists or on write failures.Error::Commandifsystemctlcommands fail.
Sourcepub fn enable(&self) -> Result<(), Error>
pub fn enable(&self) -> Result<(), Error>
Enables the service using systemctl enable.
Assumes the service file already exists and systemd has been reloaded. Requires root privileges.
Sourcepub fn start(&self) -> Result<(), Error>
pub fn start(&self) -> Result<(), Error>
Starts the service using systemctl start.
§Errors
Error::Permissionif not run with root privileges.Error::Commandif thesystemctl startcommand fails.
Sourcepub fn get_service_file_path(&self) -> Result<String, Error>
pub fn get_service_file_path(&self) -> Result<String, Error>
Sourcepub fn reload_systemd() -> Result<(), Error>
pub fn reload_systemd() -> Result<(), Error>
Reloads the systemd daemon (systemctl daemon-reload).
Requires root privileges.
Sourcepub fn stop(&self) -> Result<(), Error>
pub fn stop(&self) -> Result<(), Error>
Stop the service using systemctl start.
§Errors
Error::Permissionif not run with root privileges.Error::Commandif thesystemctl stopcommand fails.
Sourcepub fn restart(&self) -> Result<(), Error>
pub fn restart(&self) -> Result<(), Error>
Restarts the service using systemctl restart.
§Errors
Error::Permissionif not run with root privileges.Error::Commandif thesystemctl restartcommand fails.