6

So I have this cronjob on an Ubuntu 16.04 machine that ends in

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

that I use to warn all users about some action taking place. Although I redirect its output to /dev/null, the script somehow manages to output

wall: cannot get tty name: Inappropriate ioctl for device

which gets sent per e-mail to the root account. How do I make wall silent? The broadcasting works, but I don't want to have those e-mails in my root mailbox.

# wall -V
wall from util-linux 2.27.1
Pavel
  • 988
  • 1
  • 8
  • 29

2 Answers2

9

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.

Matthew Ife
  • 22,927
  • 2
  • 54
  • 71
  • Thanks. the `-n` do indeed remove the error. You have to be root to use this option. If you just want to remove the warning (and still have the banner) just add `2>/dev/null` at the end of the command (cron send you a mail if there is something on stderr) – Boop Apr 17 '18 at 07:00
1

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)".

  • I don't understand why your answer gets downvoted. This is also a perfectly reasonable solution which works very well. Thank you! – Pavel Nov 17 '16 at 16:53
  • Yeah, I don't know why it was downvoted either. For what it's worth, the accepted answer still has the redirects specified in the wrong order (which is clearly incorrect--the order absolutely matters). :-( – Thomas Dwyer III Dec 19 '16 at 19:03