gwbres / systemctl Goto Github PK
View Code? Open in Web Editor NEW:penguin: Small rust crate to interact with systemd
License: Apache License 2.0
:penguin: Small rust crate to interact with systemd
License: Apache License 2.0
Line 111 in 7009f9a
Use systemctl list-unit-files
for systemctl version systemd 245 (245.4-4ubuntu3.17)
, output the following result
Judging by the number of columns is unreasonable.
I wanted to programmatically restart a systemd service, and since I didn't want to set the SYSTEMCTL_PATH
variable, I used https://github.com/lucab/zbus_systemd instead. The advantage to using the zbus crate is that it is less hacky / unstable because it uses D-Bus instead of the CLI. Can zbus_systemd
replace all uses of this crate? Are there some scenarios where this crate is still the better choice?
Even if zbus_systemd
can't replace this crate entirely, it would still be good to mention in the README that zbus_systemd
might be the better way of controlling systemd with Rust.
After I made this issue I realized that #2 already exists, but nothing was really done with that issue, so I think replacing this crate / mentioning the zbus crate in the README should be reconsidered.
Hi, I use the lib in my project. I appreciate your work. Service enable and start would be good.
Hiya!
This package works great for 20.04 onwards. On 18.04 (and before?) however, the systemctl binary is in a different location: /bin/systemctl
.
After examining the code base, I spotted the SYSTEMCTL_PATH
environment variable that can be set. I have tried to set the variable in a multitude of ways from my application, but the variable does not seem to take effect. I verified this by cloning and using a local copy of this library.
Ways I've tried:
SYSTEMCTL_PATH=/bin/systemctl cargo r -- status
export SYSTEMCTL_PATH=/bin/systemctl && cargo r -- status
temp_env::with_var("SYSTEMCTL_PATH", Some("/bin/systemctl"), || {
systemctl::exists(SERVICE_NAME)
})?;
env::set_var
:env::set_var(key, "/bin/systemctl")
systemctl::exists(SERVICE_NAME)
In all cases, default_env!
uses the default path.
Hi, thanks for making this library! I think it's really useful to have a simple Rust library for working with systemd.
I tried following the "Service enumeration" example in the README, but it fails because list_units()
is not public:
error[E0603]: function `list_units` is private
--> src/main.rs:6:28
|
6 | let units = systemctl::list_units(None, None);
| ^^^^^^^^^^ private function
It would be awesome if it support '--user' systemd user services!
systemctl --user status foo.service
I would like to be able to create a unit from a call to systemctl::Unit::from_systemctl("sshd").unwrap()
without a bunch of information printing out to the terminal.
let services = vec!["sshd","podman"];
let disabled_services = match systemctl::list_disabled_services(){
Ok(list) => list,
Err(_) => bail!("Unable to fetch list of disabled services")
};
println!("disabled services : {:?}",disabled_services);
for service in &services {
if disabled_services.contains(&service.to_string()) {
service_not_ok.push(service);
log::warn!("service: {service} is disabled");
continue;
}
println!("{service}: {}",systemctl::Unit::from_systemctl(service).unwrap().preset);
match systemctl::Unit::from_systemctl(service) {
Ok(service_details) => {
if !service_details.active {
log::warn!("service: {service} is not running");
unforced_error=true;
}
},
Err(err) => log::error!("Error fetching {service} status: {err}"),
}
}
output is :
disabled services : []
sshd: true
podman: false
WARN greenboot > service: podman is not running
The disabled service list in empty where I expected podman
to be present.
As
[sayan@ibm-p8-kvm-03-guest-02 ~]$ systemctl list-unit-files | grep podman
podman-auto-update.service disabled disabled
podman-clean-transient.service disabled disabled
[email protected] disabled disabled
podman-restart.service disabled disabled
podman.service disabled disabled
podman.socket disabled disabled
podman-auto-update.timer disabled disabled
Hi,
I'm using this crate on a device running Linux (Yocto Dunfel), which run systemctl 244.
Where "systemctl list-unit-files" gives me:
UNIT FILE STATE
-.mount generated
boot.mount generated
data.mount generated
dev-hugepages.mount static
dev-mqueue.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount static
var-volatile.mount generated
systemd-ask-password-console.path static
systemd-ask-password-wall.path static
alsa-restore.service static
alsa-state.service static
apmd.service static
[email protected] enabled
bluetooth.service enabled
[email protected] static
busybox-klogd.service enabled
busybox-syslog.service enabled
console-getty.service disabled
[email protected] static
containerd.service disabled
crond.service enabled
dbus-org.bluez.service enabled
dbus-org.freedesktop.hostname1.service static
dbus-org.freedesktop.locale1.service static
dbus-org.freedesktop.login1.service static
dbus-org.freedesktop.network1.service enabled
dbus-org.freedesktop.resolve1.service enabled
dbus-org.freedesktop.timedate1.service static
dbus-org.freedesktop.timesync1.service enabled
dbus.service static
debug-shell.service disabled
I.e. the third column "VENDOR PRESET" doesn't exist.
So when I run e.g. "systemctl::exists" in my code it panics.
It looks lite this line is the culprit.
Ideally, for cases like these, it would be possible to manage systemd through a library instead of systemctl.
A reasonable question is, for the feature-set of this library, what would be involved in implementing the systemctl features in rust?
This code doesn't work:
const SYSTEMCTL_PATH: &str = "/usr/bin/systemctl";
With the error Error: No such file or directory (os error 2)
. This is because /usr/bin/systemctl
does not exist in NixOS. systemctl
is in the path in NixOS, so it would be better if the default path just called systemctl
instead of assuming a certain path for systemctl
(/usr/bin
should be in path anyways).
So i was trying to get a list of all units as in the doc's, but it doesn't work because returns a empty vector
fn test_status() { let units = systemctl::list_units(None, None); // all units let is_ok = units.is_ok(); println!("{}", is_ok); let new_units = units.unwrap(); println!("{}", new_units.len()); }
output
true 0
Test in the terminal
systemctl list-units | wc -l 199
I'm i doing something wrong or is something broken
I am importing using use systemctl::*;
and running the following code:
let services = list_units(None, None).unwrap();
println!("Services: {:?}", services);
and I get
Services: []
no matter what I do.
I validated that systemctl
is in /usr/bin/systemctl
and when I run the actual commands locally I do not have any problems. Am I missing something obvious here?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.