How to silently broadcast a warning from a cronjob via "wall"?

Looking at the wall source it says this:

259          if (print_banner == TRUE) {
...
...
271                  where = ttyname(STDOUT_FILENO);

This ttyname call is the cause of your issue because cron doesn't run a TTY. It is doing this because the tty name is in the banner message I.E

Broadcast message from user@host (pts/0)

Based off of the logic however it wont try this if you dont tell it to print a banner. Its trivial to avoid this problem by invoking wall like this:

if ...
  echo "warning" | wall -n 2>&1 > /dev/null
fi

This should avoid the problem entirely. You'll need to supply your own banner however in the resulting output.


The reason this isn't working the way you expect is because you have the two redirects specified in the wrong order. The order matters. You wrote:

wall 2>&1 > /dev/null

which means "redirect stderr to the same place stdout is currently going (usually a tty) and then redirect stdout to /dev/null". You should have written:

wall > /dev/null 2>&1

which means "redirect stdout to /dev/null and then redirect stderr to the same place stdout is currently going (which is now /dev/null)".