Crates.io | systemctl |
lib.rs | systemctl |
version | 0.3.1 |
source | src |
created_at | 2022-04-12 09:53:04.615885 |
updated_at | 2023-10-05 07:09:33.809576 |
description | Small crate to interact with systemd units |
homepage | https://github.com/gwbres/systemctl |
repository | |
max_upload_size | |
id | 566353 |
size | 45,743 |
Small rust crate to interact with systemd units
Currently SystemD Version <245 are not supported as unit-file-list changed from two column to three column setup. See: SystemD Changelog
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
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();
}
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*"));
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());
from_systemctl