• Stars
    star
    189
  • Rank 204,718 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provides adapters to communicate with various operating system service managers like launchd and systemd

Service Manager

Crates.io Docs CI

Rust library that provides an interface towards working with the following service management platforms:

Requires Rust 1.58.1 or higher!

Installation

Add the following to your Cargo.toml:

[dependencies]
service-manager = "0.3"

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 context
  • uninstall - will uninstall the service specified by a given context
  • start - will start an installed service specified by a given context
  • stop - will stop a running service specified by a given context
use service_manager::*;
use std::ffi::OsString;
use std::path::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 = <dyn ServiceManager>::native()
    .expect("Failed to detect management platform");

// Install our service using the underlying service management platform
manager.install(ServiceInstallCtx {
    label: label.clone(),
    program: PathBuf::from("path/to/my-service-executable"),
    args: vec![OsString::from("--some-arg")],
    contents: None, // Optional String for system-specific service content.
}).expect("Failed to install");

// Start our service using the underlying service management platform
manager.start(ServiceStartCtx {
    label: label.clone()
}).expect("Failed to start");

// Stop our service using the underlying service management platform
manager.stop(ServiceStopCtx {
    label: label.clone()
}).expect("Failed to stop");

// Uninstall our service using the underlying service management platform
manager.uninstall(ServiceUninstallCtx {
    label: label.clone()
}).expect("Failed to stop");

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 service_manager::*;

// 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 = <dyn ServiceManager>::native()
    .expect("Failed to detect management platform");

// Update our manager to work with user-level services
manager.set_level(ServiceLevel::User)
    .expect("Service manager does not support user-level services");

// 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 service_manager::*;
use std::ffi::OsString;
use std::path::PathBuf;

// Create a label for our service
let label: ServiceLabel = "com.example.my-service".parse().unwrap();

// Instantiate a specific service manager
let mut manager = LaunchdServiceManager::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(ServiceInstallCtx {
    label: label.clone(),
    program: PathBuf::from("path/to/my-service-executable"),
    args: vec![OsString::from("--some-arg")],
    contents: None, // Optional String for system-specific service content.
}).expect("Failed to install");

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:

cargo test -p system-tests systemd_for_user -- --nocapture

Separately, run a systemd system test using the following (notice using of sudo -E to maintain permissions needed for system-level installation):

sudo -E cargo test -p system-tests systemd_for_system -- --nocapture

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.

More Repositories

1

distant.nvim

🚧 (Alpha stage software) Edit files, run programs, and work with LSP on a remote machine from the comfort of your local environment 🚧
Lua
1,185
star
2

distant

🚧 (Alpha stage software) Library and tooling that supports remote filesystem and process operations. 🚧
Rust
568
star
3

choose

Fuzzy matcher for OS X that uses both std{in,out} and a native GUI
Objective-C
420
star
4

org-roam.nvim

Port of org-roam to neovim using orgmode
Lua
109
star
5

vimwiki-rs

Rust library and tooling to parse, render, and modify vimwiki text and files.
Rust
56
star
6

vimwiki.nvim

Neovim plugin that offers enhanced and alternative functionality for the vimwiki language.
Lua
50
star
7

entity-rs

A simplistic framework based on TAO, Facebook's distributed database for social graph
Rust
42
star
8

typed-path

Provides typed variants of Path and PathBuf for Unix and Windows
Rust
38
star
9

grid-side

Personal portfolio and blog for use by the Hugo generator.
CSS
32
star
10

zsh-notes

Plugin extracted from my zshrc to provide a quick notes editing experience in zsh.
Shell
18
star
11

sbt-jdi-tools

Sbt plugin to add JDI tools.jar to sbt classpath.
Scala
7
star
12

tmux-xmonadbindings

XMonad bindings for tmux
Objective-C
5
star
13

grus

Static site generator written in Scala using Scalatags for templates and Flexmark for markdown.
Scala
5
star
14

vimvar-rs

Rust library to support retrieving variables from vim & neovim
Rust
3
star
15

distant.dev

Website for distant and associated software projects
Python
3
star
16

nested-struct

Rust
3
star
17

winsplit-rs

Library to split string into command line arguments mirroring CommandLineToArgV, following VC++ 2008 parsing rules
Rust
3
star
18

get-keyboard-state

Simple program that returns keyboard codes directly from the Linux input subsystem.
C
2
star
19

makepdf

Utility to create PDFs using Lua specifically designed for supporting e-ink template generation.
Rust
2
star
20

org-mouse.nvim

Mouse features on top of nvim-orgmode.
Lua
2
star
21

printpdf

An easy-to-use library for writing PDF in Rust
Rust
1
star
22

wyrd

Fork of wyrd with patches
OCaml
1
star
23

vimdoc2html

Utility to convert vimdoc to html using tree-sitter.
Rust
1
star
24

memtable-rs

Library to provide an inmemory table for use in Rust
Rust
1
star
25

pwcheck

Rust library to check a username & password using platform-native tools.
Rust
1
star
26

goals

A list of goals I would like to accomplish.
1
star
27

homebrew-personal

Contains personal formulas for projects I work on.
Ruby
1
star