Service Manager
This crate provides a unified interface for managing system services across different platforms.
It abstracts the underlying service management system (like launchctl on macOS or systemd on Linux),
allowing you to start, stop, and monitor services with a consistent API.
Features
- A
ServiceManagertrait defining a common interface for service operations. - Platform-specific implementations for:
- macOS (
launchctl) - Linux (
systemd)
- macOS (
- A factory function
create_service_managerthat returns the appropriate manager for the current platform.
Usage
Add this to your Cargo.toml:
[dependencies]
service_manager = { path = "../service_manager" }
Here is an example of how to use the ServiceManager:
use service_manager::{create_service_manager, ServiceConfig};
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let service_manager = create_service_manager();
let config = ServiceConfig {
name: "my-service".to_string(),
binary_path: "/usr/local/bin/my-service-executable".to_string(),
args: vec!["--config".to_string(), "/etc/my-service.conf".to_string()],
working_directory: Some("/var/tmp".to_string()),
environment: HashMap::new(),
auto_restart: true,
};
// Start a new service
service_manager.start(&config)?;
// Get the status of the service
let status = service_manager.status("my-service")?;
println!("Service status: {:?}", status);
// Stop the service
service_manager.stop("my-service")?;
Ok(())
}