SystemdService

Struct SystemdService 

Source
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

Source

pub fn new(config: ServiceConfig) -> Self

Creates a new SystemdService from a given configuration.

Source

pub fn generate(&self) -> String

Generates the content of the .service unit file as a string.

Source

pub fn write(&self, path: &Path) -> Result<(), Error>

Writes the generated service file content to the specified path.

§Errors
Source

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:

  1. Writes the service file to /etc/systemd/system/.
  2. Reloads the systemd daemon (systemctl daemon-reload).
  3. Enables the service (systemctl enable).
§Errors
Source

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.

Source

pub fn start(&self) -> Result<(), Error>

Starts the service using systemctl start.

§Errors
Source

pub fn get_service_file_path(&self) -> Result<String, Error>

Gets the conventional path for the service file (e.g., /etc/systemd/system/myapp.service).

§Errors
  • Error::Io if the service file already exists at this path.
Source

pub fn reload_systemd() -> Result<(), Error>

Reloads the systemd daemon (systemctl daemon-reload).

Requires root privileges.

Source

pub fn stop(&self) -> Result<(), Error>

Stop the service using systemctl start.

§Errors
Source

pub fn restart(&self) -> Result<(), Error>

Restarts the service using systemctl restart.

§Errors

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.