Service Manager
Rust library that provides an interface towards working with the following service management platforms:
sc.exe
for use with Window Service (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.1"
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 ;
// 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 *;
// 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 = false;
// Install our service using the explicit service manager
manager.install.expect;
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.