Run a program when new line appears in dmesg

130 Views Asked by At
sudo dmesg -w|grep "Manufacturer: Keychron K1"| xargs -I{} xset r rate 250 70

It does not work, why?

I am trying to reset keyboard settings when the keyboard is reconnected, but I cannot get dmesg -w|xargs... combination to work. It supposed to be working, for example if I do this:

while :; do echo tick; sleep 1s; done|xargs -I{} date

I will get a new time printed every second. The idea was to use dmesg -w then grep then xargs but it does not work until xargs is killed from another terminal.

Why and how to fix?

2

There are 2 best solutions below

0
datenwolf On

You're asking an XY(Z) problem here, see "What is the XY problem?".

Your problem X is "how do I automatically have input devices configured in Xorg when hotplugged?"

And for that you simply want to write a xorg.conf.d InputClass rule that will make the appropriate settings for your keyboard:

https://www.x.org/releases/current/doc/man/man5/xorg.conf.5.xhtml#heading9


However you misidentified this problem as problem Y "how to automatically execute a program upon hotplug?" – for that we have UDev

For that problem look at UDev rules: https://wiki.archlinux.org/title/Udev


However misidentified problem Y as the "problem" Z "how can I execute the last part of a chain if pipe redirections, when a certain string appears". Tackling that does not solve your actual problem.

This

sudo dmesg -w | grep … | xargs …

pipes three programs together, which are all executing at the same time. xargs waits for the end of input and then executes whatever was passed to it as parameters. Of course dmesg -w will never produce an end-of-file.

0
KamilCuk On

Why

Buffering.

how to fix?

Set line buffering of tools with stdbuf or grep --line-buffered.