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: PathBufThe 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: boolWhether the service should start automatically when the system boots or user logs in.
restart_on_failure: boolWhether 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
impl ServiceSpec
Sourcepub fn new(path: impl Into<PathBuf>) -> Self
pub fn new(path: impl Into<PathBuf>) -> Self
Creates a new service specification with the given path to the executable.
Examples found in repository?
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}Sourcepub fn arg(self, arg: impl Into<OsString>) -> UniResult<Self, ServiceErrKind>
pub fn arg(self, arg: impl Into<OsString>) -> UniResult<Self, ServiceErrKind>
Adds an argument to the executable.
Examples found in repository?
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}Sourcepub fn display_name(
self,
display_name: impl Into<OsString>,
) -> UniResult<Self, ServiceErrKind>
pub fn display_name( self, display_name: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>
Sets the display name of the service.
Examples found in repository?
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}Sourcepub fn description(
self,
desc: impl Into<OsString>,
) -> UniResult<Self, ServiceErrKind>
pub fn description( self, desc: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>
Sets the description of the service.
Examples found in repository?
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}Sourcepub fn set_autostart(self) -> Self
pub fn set_autostart(self) -> Self
Sets whether the service should start automatically when the system boots or user logs in.
Sourcepub fn set_restart_on_failure(self) -> Self
pub fn set_restart_on_failure(self) -> Self
Sets whether the service should be restarted if it fails.
Sourcepub fn set_user(
self,
user: impl Into<OsString>,
) -> UniResult<Self, ServiceErrKind>
pub fn set_user( self, user: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>
Sets the user to run the service as.
Sourcepub fn set_password(
self,
password: impl Into<OsString>,
) -> UniResult<Self, ServiceErrKind>
pub fn set_password( self, password: impl Into<OsString>, ) -> UniResult<Self, ServiceErrKind>
Sets the password to use for the user.