use sal_service_manager::{create_service_manager, ServiceConfig}; use std::collections::HashMap; use std::thread; use std::time::Duration; fn main() { // 1. Create a service manager for the current platform let manager = match create_service_manager() { Ok(manager) => manager, Err(e) => { eprintln!("Error: Failed to create service manager: {}", e); return; } }; // 2. Define the configuration for our new service let service_name = "com.herocode.examples.simpleservice"; let service_config = ServiceConfig { name: service_name.to_string(), // A simple command that runs in a loop binary_path: "/bin/sh".to_string(), args: vec![ "-c".to_string(), "while true; do echo 'Simple service is running...'; date; sleep 5; done".to_string(), ], working_directory: None, environment: HashMap::new(), auto_restart: false, }; println!("--- Service Manager Example ---"); // Cleanup from previous runs, if necessary if let Ok(true) = manager.exists(service_name) { println!( "Service '{}' already exists. Cleaning up before starting.", service_name ); if let Err(e) = manager.stop(service_name) { println!( "Note: could not stop existing service (it might not be running): {}", e ); } if let Err(e) = manager.remove(service_name) { eprintln!("Error: failed to remove existing service: {}", e); return; } println!("Cleanup complete."); } // 3. Start the service (creates and starts in one step) println!("\n1. Starting service: '{}'", service_name); match manager.start(&service_config) { Ok(()) => println!("Service '{}' started successfully.", service_name), Err(e) => { eprintln!("Error: Failed to start service '{}': {}", service_name, e); return; } } // Give it a moment to run println!("\nWaiting for 2 seconds for the service to initialize..."); thread::sleep(Duration::from_secs(2)); // 4. Check the status of the service println!("\n2. Checking service status..."); match manager.status(service_name) { Ok(status) => println!("Service status: {:?}", status), Err(e) => eprintln!( "Error: Failed to get status for service '{}': {}", service_name, e ), } println!("\nLetting the service run for 10 seconds. Check logs if you can."); thread::sleep(Duration::from_secs(10)); // 5. Stop the service println!("\n3. Stopping service: '{}'", service_name); match manager.stop(service_name) { Ok(()) => println!("Service '{}' stopped successfully.", service_name), Err(e) => eprintln!("Error: Failed to stop service '{}': {}", service_name, e), } println!("\nWaiting for 2 seconds for the service to stop..."); thread::sleep(Duration::from_secs(2)); // Check status again println!("\n4. Checking status after stopping..."); match manager.status(service_name) { Ok(status) => println!("Service status: {:?}", status), Err(e) => eprintln!( "Error: Failed to get status for service '{}': {}", service_name, e ), } // 6. Remove the service println!("\n5. Removing service: '{}'", service_name); match manager.remove(service_name) { Ok(()) => println!("Service '{}' removed successfully.", service_name), Err(e) => eprintln!("Error: Failed to remove service '{}': {}", service_name, e), } println!("\n--- Example Finished ---"); }