How to watch for stdout changes of a command (original) (raw)
February 3, 2025, 7:15am 1
I’m using timedatectl
to watch for ntp changes. The following is the code:
args = ["sh", "-c", "timedatectl --monitor timesync-status"]
# Start process
pid, stdin_fd, stdout_fd, stderr_fd = GLib.spawn_async(
args,
flags=GLib.SpawnFlags.DO_NOT_REAP_CHILD,
standard_output=True,
standard_error=True
# Create IOChannel for stdout
stdout_channel = GLib.IOChannel.unix_new(stdout_fd)
stdout_channel.set_encoding(None) # Read raw bytes
stdout_channel.add_watch(GLib.IO_IN | GLib.IO_HUP, lambda *args: print("changed"))
)
But it only prints “changed” once
The actual command I intend to use here is timedatectl --monitor timesync-status | grep --line-buffered "Poll interval" | awk '{print $3}' fflush()
which outputs either 0s or something else. I tried with this command but got same results
jensgeorg (Jens Georg) February 3, 2025, 7:51am 2
- if you want to use GLib, I’d recommend to switch to GIO’s Subprocess, which is much nicer to work with
- Why would you use GLib at all here, when there is Python.subprocess?
pwithnall (Philip Withnall) February 3, 2025, 9:44am 3
If you can use the libc timerfd_*()
APIs from Python (which, from a quick search, seems to be possible) then you can be notified of system clock changes directly, without needing to run a subprocess.
There’s an example of this here
raVen (Abir Halder) February 3, 2025, 11:10am 4
I actually didn’t check Python.subprocess
but that’s a nice idea actually. My initial goal was to keep everything within PyGobject
system (system) Closed March 5, 2025, 11:10am 5
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.