simohamed
simohamed

Reputation: 81

How to pipe stderr to notify-send?

I run programs using keybindings, which themselves could call dmenu and do things in the background. I would love to pipe stderr to notify-send and get a notification if something failed.

These programs are not called from a terminal emulator, mostly sxhkd coupled with an xdg-open alternative jaro.

Take this example, for instance:

$ ls /root
"/root": Permission denied (os error 13)
$ ls /root 2> /dev/null
$

If I had an sxhkd entry

super + Return
  ls /root

How can I have it output stderr to a notification?

Upvotes: 1

Views: 1879

Answers (3)

water_elixir
water_elixir

Reputation: 11

Use xargs to effectively pass something in arguments, allows more independent execution to add more arguments and configuration for notify-send.

[command] 2>&1 | xargs -I {} notify-send "{}"

Example: ls "i don't exist" 2>&1 | xargs -I {} notify-send "{}"

Upvotes: 0

simohamed
simohamed

Reputation: 81

Create a script notify-pipe:

#!/usr/bin/env sh

read notification
notify-send "Command Failed" "$notification" "$@"

And pipe stderr to stdout:

$ ls /root  2>&1| notify-pipe

Possible enhancement: bash get command that was used before pipe symbol to get the command in the notification summary.

Thanks for the help, Ron.

Upvotes: 1

Ron
Ron

Reputation: 6551

notify-send will not work with piping

However, you can try: sudo pip install notify-pipe

Which accepts piping

See here: https://github.com/ron7/notify-pipe

And you can pipe stderr to stdout and send it to notify-pipe

command 2>&1|notify-pipe

Upvotes: 1

Related Questions