ServiceSpec

Struct ServiceSpec 

Source
pub struct ServiceSpec {
    pub path: PathBuf,
    pub args: Vec<OsString>,
    pub display_name: Option<OsString>,
    pub description: Option<OsString>,
    pub autostart: bool,
    pub restart_on_failure: bool,
    pub user: Option<OsString>,
    pub password: Option<OsString>,
    pub group: Option<OsString>,
}
Expand description

A specification of a service to be installed.

Fields§

§path: PathBuf

The path to the executable to run when the service starts.

§args: Vec<OsString>

The arguments to pass to the executable.

§display_name: Option<OsString>

The display name of the service.

§description: Option<OsString>

The description of the service.

§autostart: bool

Whether the service should start automatically when the system boots or user logs in.

§restart_on_failure: bool

Whether the service should be restarted if it fails.

§user: Option<OsString>

User to run the service as.

§password: Option<OsString>

Password to use for the user.

§group: Option<OsString>

Group to run the service as.

Implementations§

Source§

impl ServiceSpec

Source

pub fn new(path: impl Into<PathBuf>) -> Self

Creates a new service specification with the given path to the executable.

Examples found in repository?
examples/manager.rs (line 30)
8fn run(
9    service_name: &str,
10    display_name: &str,
11    description: &str,
12) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
13    // Windows user services require a new logon before they can be started, so we will use a system service on Windows.
14    // NOTE: Windows services always require elevated privileges to install, so run this example as administrator.
15    let user = !UniServiceManager::capabilities()
16        .contains(ServiceCapabilities::USER_SERVICES_REQUIRE_NEW_LOGON);
17    let user_manager = UniServiceManager::new(service_name, "com.example.", user)?;
18
19    let mut bin_path = std::env::current_exe().unwrap();
20    bin_path.set_file_name(service_name);
21
22    if !bin_path.exists() {
23        return Err(format!(
24            "Executable not found: {}. Make sure to build the service examples first.",
25            bin_path.display()
26        )
27        .into());
28    }
29
30    let spec = ServiceSpec::new(bin_path)
31        .arg("service")?
32        .display_name(display_name)?
33        .description(description)?;
34
35    print!("Installing service '{service_name}'...",);
36    user_manager.install_and_wait(&spec, TIMEOUT)?;
37    println!("done");
38
39    print!("Starting service '{service_name}'...");
40    user_manager.start_and_wait(TIMEOUT)?;
41    println!("done");
42
43    io::stdout().flush()?;
44    println!("Press Enter to stop the service...");
45    let mut buffer = String::new();
46    io::stdin().read_line(&mut buffer)?;
47
48    print!("Stopping service '{service_name}'...");
49    user_manager.stop_and_wait(TIMEOUT)?;
50    println!("done");
51
52    print!("Uninstalling service '{service_name}'...");
53    user_manager.uninstall_and_wait(TIMEOUT)?;
54    println!("done");
55
56    Ok(())
57}
Source

pub fn arg(self, arg: impl Into<OsString>) -> UniResult<Self, ServiceErrKind>

Adds an argument to the executable.

Examples found in repository?
examples/manager.rs (line 31)
8fn run(
9    service_name: &str,
10    display_name: &str,
11    description: &str,
12) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
13    // Windows user services require a new logon before they can be started, so we will use a system service on Windows.
14    // NOTE: Windows services always require elevated privileges to install, so run this example as administrator.
15    let user = !UniServiceManager::capabilities()
16        .contains(ServiceCapabilities::USER_SERVICES_REQUIRE_NEW_LOGON);
17    let user_manager = UniServiceManager::new(service_name, "com.example.", user)?;
18
19    let mut bin_path = std::env::current_exe().unwrap();
20    bin_path.set_file_name(service_name);
21
22    if !bin_path.exists() {
23        return Err(format!(
24            "Executable not found: {}. Make sure to build the service examples first.",
25            bin_path.display()
26        )
27        .into());
28    }
29
30    let spec = ServiceSpec::new(bin_path)
31        .arg("service")?
32        .display_name(display_name)?
33        .description(description)?;
34
35    print!("Installing service '{service_name}'...",);
36    user_manager.install_and_wait(&spec, TIMEOUT)?;
37    println!("done");
38
39    print!("Starting service '{service_name}'...");
40    user_manager.start_and_wait(TIMEOUT)?;
41    println!("done");
42
43    io::stdout().flush()?;
44    println!("Press Enter to stop the service...");
45    let mut buffer = String::new();
46    io::stdin().read_line(&mut buffer)?;
47
48    print!("Stopping service '{service_name}'...");
49    user_manager.stop_and_wait(TIMEOUT)?;
50    println!("done");
51
52    print!("Uninstalling service '{service_name}'...");
53    user_manager.uninstall_and_wait(TIMEOUT)?;
54    println!("done");
55
56    Ok(())
57}
Source

pub fn display_name( self, display_name: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>

Sets the display name of the service.

Examples found in repository?
examples/manager.rs (line 32)
8fn run(
9    service_name: &str,
10    display_name: &str,
11    description: &str,
12) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
13    // Windows user services require a new logon before they can be started, so we will use a system service on Windows.
14    // NOTE: Windows services always require elevated privileges to install, so run this example as administrator.
15    let user = !UniServiceManager::capabilities()
16        .contains(ServiceCapabilities::USER_SERVICES_REQUIRE_NEW_LOGON);
17    let user_manager = UniServiceManager::new(service_name, "com.example.", user)?;
18
19    let mut bin_path = std::env::current_exe().unwrap();
20    bin_path.set_file_name(service_name);
21
22    if !bin_path.exists() {
23        return Err(format!(
24            "Executable not found: {}. Make sure to build the service examples first.",
25            bin_path.display()
26        )
27        .into());
28    }
29
30    let spec = ServiceSpec::new(bin_path)
31        .arg("service")?
32        .display_name(display_name)?
33        .description(description)?;
34
35    print!("Installing service '{service_name}'...",);
36    user_manager.install_and_wait(&spec, TIMEOUT)?;
37    println!("done");
38
39    print!("Starting service '{service_name}'...");
40    user_manager.start_and_wait(TIMEOUT)?;
41    println!("done");
42
43    io::stdout().flush()?;
44    println!("Press Enter to stop the service...");
45    let mut buffer = String::new();
46    io::stdin().read_line(&mut buffer)?;
47
48    print!("Stopping service '{service_name}'...");
49    user_manager.stop_and_wait(TIMEOUT)?;
50    println!("done");
51
52    print!("Uninstalling service '{service_name}'...");
53    user_manager.uninstall_and_wait(TIMEOUT)?;
54    println!("done");
55
56    Ok(())
57}
Source

pub fn description( self, desc: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>

Sets the description of the service.

Examples found in repository?
examples/manager.rs (line 33)
8fn run(
9    service_name: &str,
10    display_name: &str,
11    description: &str,
12) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
13    // Windows user services require a new logon before they can be started, so we will use a system service on Windows.
14    // NOTE: Windows services always require elevated privileges to install, so run this example as administrator.
15    let user = !UniServiceManager::capabilities()
16        .contains(ServiceCapabilities::USER_SERVICES_REQUIRE_NEW_LOGON);
17    let user_manager = UniServiceManager::new(service_name, "com.example.", user)?;
18
19    let mut bin_path = std::env::current_exe().unwrap();
20    bin_path.set_file_name(service_name);
21
22    if !bin_path.exists() {
23        return Err(format!(
24            "Executable not found: {}. Make sure to build the service examples first.",
25            bin_path.display()
26        )
27        .into());
28    }
29
30    let spec = ServiceSpec::new(bin_path)
31        .arg("service")?
32        .display_name(display_name)?
33        .description(description)?;
34
35    print!("Installing service '{service_name}'...",);
36    user_manager.install_and_wait(&spec, TIMEOUT)?;
37    println!("done");
38
39    print!("Starting service '{service_name}'...");
40    user_manager.start_and_wait(TIMEOUT)?;
41    println!("done");
42
43    io::stdout().flush()?;
44    println!("Press Enter to stop the service...");
45    let mut buffer = String::new();
46    io::stdin().read_line(&mut buffer)?;
47
48    print!("Stopping service '{service_name}'...");
49    user_manager.stop_and_wait(TIMEOUT)?;
50    println!("done");
51
52    print!("Uninstalling service '{service_name}'...");
53    user_manager.uninstall_and_wait(TIMEOUT)?;
54    println!("done");
55
56    Ok(())
57}
Source

pub fn set_autostart(self) -> Self

Sets whether the service should start automatically when the system boots or user logs in.

Source

pub fn set_restart_on_failure(self) -> Self

Sets whether the service should be restarted if it fails.

Source

pub fn set_user( self, user: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>

Sets the user to run the service as.

Source

pub fn set_password( self, password: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>

Sets the password to use for the user.

Source

pub fn set_group( self, group: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>

Sets the group to run the service as.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.