New
On Linux the command:

```bash
gdbus monitor -y -d org.freedesktop.login1 | grep -m 1 LockedHint
```

returns:

```
/org/freedesktop/login1/session/_35: org.freedesktop.DBus.Properties.PropertiesChanged ('org.freedesktop.login1.Session', {'LockedHint': <true>}, @as [])
```

but still hangs, why?
Nov 16, 3:03pm

The command you’re using:

bash
gdbus monitor -y -d org.freedesktop.login1 | grep -m 1 LockedHint

hangs because of how gdbus monitor and grep interact:

Why it hangs

  • gdbus monitor continuously streams D-Bus signals in real-time and does not exit on its own. It waits for new events indefinitely.
  • grep -m 1 will exit after the first match, but only if the input stream closes. Since gdbus monitor keeps running and the pipe remains open, grep cannot know if more input is coming. Thus, it waits for the stream to end, which never happens.

How to fix it

To make the command exit after the first match, you need to ensure that gdbus monitor exits after the relevant event. You can do this by using a tool like stdbuf or timeout, or by using a more sophisticated approach with awk or sed to close the stream after the first match.

Example with timeout:

bash
timeout 10 gdbus monitor -y -d org.freedesktop.login1 | grep -m 1 LockedHint

This will force gdbus monitor to exit after 10 seconds, allowing grep to finish.

Example with awk:

bash
gdbus monitor -y -d org.freedesktop.login1 | awk '/LockedHint/ {print; exit}'

This will print the first line containing "LockedHint" and then exit, closing the pipe.

Example with stdbuf (if buffering is an issue):

bash
stdbuf -oL gdbus monitor -y -d org.freedesktop.login1 | grep -m 1 LockedHint

This ensures line buffering, but the command may still hang unless the stream is closed.

Summary

The hang is due to gdbus monitor not terminating, so grep keeps waiting. Use timeout, awk, or another method to force the stream to close after the first match.

Nov 16, 3:03pm

This Chat is read-only. Login to resume chatting.