14 unstable releases (3 breaking)

0.3.1 Oct 5, 2023
0.3.0 Jul 23, 2023
0.2.0 Jul 19, 2023
0.1.9 Dec 30, 2022
0.1.7 Jul 1, 2022

#168 in Unix APIs

Download history 700/week @ 2023-12-13 401/week @ 2023-12-20 539/week @ 2023-12-27 804/week @ 2024-01-03 525/week @ 2024-01-10 698/week @ 2024-01-17 1060/week @ 2024-01-24 884/week @ 2024-01-31 789/week @ 2024-02-07 800/week @ 2024-02-14 1027/week @ 2024-02-21 786/week @ 2024-02-28 1046/week @ 2024-03-06 1115/week @ 2024-03-13 1051/week @ 2024-03-20 352/week @ 2024-03-27

3,688 downloads per month
Used in 4 crates (3 directly)

MIT/Apache

32KB
572 lines

systemctl

Small rust crate to interact with systemd units

crates.io License License crates.io
Rust crates.io

Features

  • serde: Enable to make structs in this crate De-/Serializable

Limitations

Currently SystemD Version <245 are not supported as unit-file-list changed from two column to three column setup. See: SystemD Changelog

Environment

SYSTEMCTL_PATH custom env. variable describes the absolute location path of systemctl binary, by default this crate uses /usr/bin/systemctl, but that can be customized:

SYSTEMCTL_PATH=/home/$me/bin/systemctl cargo build

Unit / service operation

Nominal service operations:

systemctl::stop("systemd-journald.service")
    .unwrap();
systemctl::restart("systemd-journald.service")
    .unwrap();

if let Ok(true) = systemctl::exists("ntpd") {
    let is_active = systemctl::is_active("ntpd")
        .unwrap();
}

Service enumeration

use systemctl;
// list all units
systemctl::list_units(None, None, None);

// list all services 
// by adding a --type filter
systemctl::list_units(Some("service"), None, None);

// list all services currently `enabled` 
// by adding a --state filter
systemctl::list_units(Some("service"), Some("enabled"), None);

// list all services starting with cron
systemctl::list_units(Some("service"), None, Some("cron*"));

Unit structure

Use the unit structure for more information

let unit = systemctl::Unit::from_systemctl("sshd")
    .unwrap();
unit.restart().unwrap();
println!("active: {}", unit.active);
println!("preset: {}", unit.preset);

if let Some(docs) = unit.docs { // doc pages available
    for doc in docs {
        if let Some(page) = doc.as_man() {
            // `man` page exists 
        }
        if let Some(url) = doc.as_url() {
            // `url` is indicated
        }
    }
}

println!("auto_start (enabled): {}", unit.auto_start);
println!("config script : {}", unit.script);
println!("pid: {}", unit.pid);
println!("Running task(s): {}", unit.tasks.unwrap());
println!("Memory consumption: {}", unit.memory.unwrap());

TODO

  • parse all known attributes in from_systemctl

Dependencies

~0.8–1.3MB
~29K SLoC