How to watch for stdout changes of a command

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

  • 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?

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

I actually didn’t check Python.subprocess but that’s a nice idea actually. My initial goal was to keep everything within PyGobject