systemctl

Small rust crate to interact with systemd units

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

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:

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

Unit / service operation

Nominal service operations:

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

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

Service enumeration

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

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

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

Unit structure

Use the unit structure for more information

```rust 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.asman() { // man page exists } if let Some(url) = doc.asurl() { // url is indicated } } }

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

TODO