systemd-service 0.3.1

Generate service files for the application
Documentation
  • Coverage
  • 87.8%
    36 out of 41 items documented1 out of 24 items with examples
  • Size
  • Source code size: 21.28 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.32 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • iKeepLearn/systemd-service
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • iKeepLearn

systemd-service

A library for Generate, installing, and managing systemd services on Linux for Rust binary.

This crate provides a fluent builder (ServiceConfig) to define a systemd service unit and a SystemdService struct to handle the generation, installation, and management (start, enable) of that service.

⚠️ Important: Requires Root Privileges

Most operations in this crate (writing to /etc/systemd/system, running systemctl commands) require root privileges to execute. The methods will return an [Error::Permission] if run by a non-root user.

Example Usage


use systemd_service::{ServiceConfig, SystemdService, Error};

fn setup_my_service() -> Result<(), Error> {
    // 1. Define the service configuration using the builder
    let config = ServiceConfig::new(
        "myapp",
        "/usr/local/bin/myapp --run",
        "My Application Service",
    )
    .user("myapp-user")
    .group("myapp-group")
    .working_directory("/var/lib/myapp")
    .after(vec!["network.target".to_string()])
    .environment(vec![
        ("RUST_LOG".to_string(), "info".to_string()),
        ("PORT".to_string(), "8080".to_string()),
    ])
    .restart("on-failure")
    .restart_sec(10);

    // 2. Create the service manager
    let service = SystemdService::new(config);

    // 3. Install, enable, and reload systemd
    // This requires root privileges!
    service.install_and_enable()?;

    // 4. Start the service
    // This also requires root privileges!
    service.start()?;

    println!("Service 'myapp' installed and started successfully.");
    Ok(())
}