systemd-client

[systemd dbus] client lib using [zbus]

Examples

Blocking

list units ```rust use systemd_client::{manager, models::Unit, Result};

fn main() -> Result<()> { let client = manager::buildblockingproxy()?; let units = client.listunits()?; for unit in units { let unit: Unit = unit.into(); println!("{:#?}", unit); } Ok(()) } create and start service rust use systemdclient::{ createunitconfiguration_file, manager, unit, Result, ServiceConfiguration, ServiceUnitConfiguration, UnitActiveStateType, UnitConfiguration, UnitLoadStateType, UnitProps, UnitSubStateType, };

/* * Run example as superuser since we start a service * sh * cargo build --example start_service_blocking * sudo ./target/debug/examples/start_service_blocking * */ fn main() -> Result<()> { let unitbuilder = UnitConfiguration::builder().description("test service"); let svcbuilder = ServiceConfiguration::builder().execstart(vec!["/bin/sleep", "3"]); let svcunit = ServiceUnitConfiguration::builder() .unit(unitbuilder) .service(svcbuilder) .build(); let svcunitliteral = format!("{}", svcunit); // create /etc/systemd/system/test.service createunitconfigurationfile("test.service", svcunitliteral.asbytes())?; let client = manager::buildblockingproxy()?; let jobpath = client.startunit("test.service", "replace")?; println!("{}", jobpath.asstr()); let svcunitpath = client.getunit("test.service")?; println!("{}", svcunitpath.asstr()); // verify unit state given unit path let client = unit::buildblockingproxy(svcunitpath)?; let unitprops = client.getproperties()?; let unitprops: UnitProps = unitprops.into(); println!("{:?}", unitprops); asserteq!(unitprops.loadstate, UnitLoadStateType::Loaded); asserteq!(unitprops.activestate, UnitActiveStateType::Active); asserteq!(unitprops.substate, UnitSubStateType::Running); std::thread::sleep(std::time::Duration::fromsecs(4)); // service should exit after 3 sec let unitprops = client.getproperties()?; let unitprops: UnitProps = unitprops.into(); println!("{:?}", unitprops); asserteq!(unitprops.loadstate, UnitLoadStateType::Loaded); asserteq!(unitprops.activestate, UnitActiveStateType::Inactive); asserteq!(unitprops.substate, UnitSubStateType::Dead); Ok(()) } ```

Non Block

list units ```rust use systemd_client::{manager, models::Unit, Result};

[tokio::main]

pub async fn main() -> Result<()> { let client = manager::buildnonblockproxy().await?; let units = client.listunits().await?; for unit in units { let unit: Unit = unit.into(); println!("{:#?}", unit); } Ok(()) } create and start service rust use systemdclient::{ createunitconfiguration_file, manager, unit, Result, ServiceConfiguration, ServiceUnitConfiguration, UnitActiveStateType, UnitConfiguration, UnitLoadStateType, UnitSubStateType, };

/* * Run example as superuser since we start a service * sh * cargo build --example start_service_nonblock * sudo ./target/debug/examples/start_service_nonblock * */

[tokio::main]

async fn main() -> Result<()> { let unitbuilder = UnitConfiguration::builder().description("test service"); let svcbuilder = ServiceConfiguration::builder().execstart(vec!["/bin/sleep", "3"]); let svcunit = ServiceUnitConfiguration::builder() .unit(unitbuilder) .service(svcbuilder) .build(); let svcunitliteral = format!("{}", svcunit); // create /etc/systemd/system/test.service createunitconfigurationfile("test.service", svcunitliteral.asbytes())?; let client = manager::buildnonblockproxy().await?; let jobpath = client.startunit("test.service", "replace").await?; println!("{}", jobpath.asstr()); let svcunitpath = client.getunit("test.service").await?; println!("{}", svcunitpath.asstr()); // verify unit state given unit path let client = unit::buildnonblockproxy(svcunitpath).await?; let unitprops = client.getproperties().await?; println!("{:?}", unitprops); asserteq!(unitprops.loadstate, UnitLoadStateType::Loaded); asserteq!(unitprops.activestate, UnitActiveStateType::Active); asserteq!(unitprops.substate, UnitSubStateType::Running); std::thread::sleep(std::time::Duration::fromsecs(4)); // service should exit after 3 sec let unitprops = client.getproperties().await?; println!("{:?}", unitprops); asserteq!(unitprops.loadstate, UnitLoadStateType::Loaded); asserteq!(unitprops.activestate, UnitActiveStateType::Inactive); asserteq!(unitprops.substate, UnitSubStateType::Dead); Ok(()) } ```