Service Manager
Rust library that provides an interface towards working with the following service management platforms:
sc.exefor use with Window Service (Windows)- Winsw (Windows)
- Launchd (MacOS)
- systemd (Linux)
- OpenRC (Linux)
- rc.d (FreeBSD)
Requires Rust 1.58.1 or higher!
Installation
Add the following to your Cargo.toml:
[]
= "0.8"
Examples
Generic service management
This crate provides a mechanism to detect and use the default service
management platform of the current operating system. Each ServiceManager
instance provides four key methods:
install- will install the service specified by a given contextuninstall- will uninstall the service specified by a given contextstart- will start an installed service specified by a given contextstop- will stop a running service specified by a given context
use *;
use OsString;
use PathBuf;
// Create a label for our service
let label: ServiceLabel = "com.example.my-service".parse.unwrap;
// Get generic service by detecting what is available on the platform
let manager = native
.expect;
// Install our service using the underlying service management platform
manager.install.expect;
// Start our service using the underlying service management platform
manager.start.expect;
// Stop our service using the underlying service management platform
manager.stop.expect;
// Uninstall our service using the underlying service management platform
manager.uninstall.expect;
User-level service management
By default, service management platforms will interact with system-level
services; however, some service management platforms like systemd and
launchd support user-level services. To interact with services at the
user level, you configure your manager using the generic
ServiceManager::set_level function.
use *;
// Create a label for our service
let label: ServiceLabel = "com.example.my-service".parse.unwrap;
// Get generic service by detecting what is available on the platform
let mut manager = native
.expect;
// Update our manager to work with user-level services
manager.set_level
.expect;
// Continue operating as usual via install/uninstall/start/stop
// ...
Specific service manager configurations
There are times where you need more control over the configuration of a service tied to a specific platform. To that end, you can create the service manager explicitly and set configuration properties appropriately.
use *;
use OsString;
use PathBuf;
// Create a label for our service
let label: ServiceLabel = "com.example.my-service".parse.unwrap;
// Instantiate a specific service manager
let mut manager = system;
// Update an install configuration property where installing a service
// will NOT add the KeepAlive flag
manager.config.install.keep_alive = Some;
// Install our service using the explicit service manager
manager.install.expect;
Configuring restart policies
The crate provides a cross-platform RestartPolicy enum that allows you to control
when and how services should be restarted. Different platforms support different levels
of granularity, and the implementation will use the closest approximation when an exact
match isn't available.
If you need options specific to any given service manager, you should use that specific
service manager rather than the generic ServiceManager crate.
use *;
use OsString;
use PathBuf;
let label: ServiceLabel = "com.example.my-service".parse.unwrap;
let manager = native
.expect;
// Example 1: Never restart the service
manager.install.expect;
// Example 2: Always restart regardless of exit status
manager.install.expect;
// Example 3: Restart only on failure (non-zero exit)
manager.install.expect;
Platform support:
- systemd (Linux): Supports all restart policies natively
- launchd (macOS): Only supports Never vs Always/OnFailure (uses KeepAlive boolean)
- WinSW (Windows): Supports all restart policies
- OpenRC/rc.d/sc.exe: Limited or no restart support; warnings logged for unsupported policies
Running tests
For testing purposes, we use a separate crate called system-tests and
execute singular tests based on desired platform and level. From the root of
the repository, execute the following to run a systemd user test:
Separately, run a systemd system test using the following (notice using of
sudo -E to maintain permissions needed for system-level installation):
License
This project is licensed under either of
Apache License, Version 2.0, (LICENSE-APACHE or apache-license) MIT license (LICENSE-MIT or mit-license) at your option.