How do I show an alert in virtual console ttyX (not necessarily the active one) so that the user sees the alert on the next command invocation. I'm looking for something similar to the "you have mail" alert.

  • Would the target user be logged on to that vty? – roaima Feb 28 '17 at 21:55
  • What do you mean by “on the next command invocation”? Do you mean that there is a user running a shell on that console and you want to display the alert the next time the shell shows a prompt? Are you allowed to change their shell configuration? If not that would be difficult: the mail alert works because the shell prints it, another program wouldn't know when to print it. – Gilles 'SO- stop being evil' Feb 28 '17 at 22:17
  • Yes, I assume that a user is logged in on the virtual terminal. I have root access to the computer in question. I'm implementing a low battery alert that is supposed to work under all conditions. – August Karlstrom Feb 28 '17 at 22:56

If you know which user is logged in on which virtual console, you can use write. E.g., assume user dirk is logged in on tty2, you can do

echo 'You have a message' | write dirk tty2

and the user will see the message (with two additional lines). The user on the virtual console needs to enable receiving messages with mesg y, unless you send the message as root IIRC. The user sees this message immediately, no matter if he invokes a command or not.

The alternative would be to hook into the shell of the user (possible using the PS prompts) by setting up .profile etc. to actively check for messages in some file etc.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.