15 releases

0.4.0 Sep 26, 2024
0.3.1 Oct 5, 2023
0.3.0 Jul 23, 2023
0.1.9 Dec 30, 2022
0.1.7 Jul 1, 2022

#119 in Unix APIs

Download history 1146/week @ 2024-08-22 1241/week @ 2024-08-29 828/week @ 2024-09-05 798/week @ 2024-09-12 738/week @ 2024-09-19 1243/week @ 2024-09-26 1508/week @ 2024-10-03 1011/week @ 2024-10-10 1205/week @ 2024-10-17 915/week @ 2024-10-24 875/week @ 2024-10-31 773/week @ 2024-11-07 758/week @ 2024-11-14 902/week @ 2024-11-21 928/week @ 2024-11-28 741/week @ 2024-12-05

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

MIT/Apache

32KB
563 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

Unit / service operation

Nominal service operations:

let systemctl = systemctl::SystemCtl::default();
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

let systemctl = systemctl::SystemCtl::default();
// 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 systemctl = systemctl::SystemCtl::default();
let unit = systemctl.create_unit("ssh.service")
    .unwrap();
systemctl.restart(&unit.name).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);
println!("Memory consumption: {:?}", unit.memory);

TODO

  • parse all known attributes in from_systemctl

Dependencies

~1–1.7MB
~34K SLoC