OK, here’s something fun for you guys to experiment with. I’ll show you how to send messages across terminals on a Linux system. This is can be quite useful if you have to suddenly shut down the system and need to inform all logged in users. though, the shutdown command has a built-in function to take care of just that. This is what you’ll need to pass messages:
- A Message, first and foremost . Even a ‘hi’ will do for now.
- One echo command.
- A list of all the terminals that the users logged in at. This can be provided by who.
- A little bit of output redirection!
Here’s how you find out the terminals that the users are logged in from. This is a sample output, not a real one:
Thus, the users are logged in at the terminals tty1, tty2, pts/0 (or :0, Xorg’s way of specifying display number) and pts/1. The last line shows root to be remotely logged into the system from a IP Address 220.127.116.11, which has been allocated pts/1 as the terminal number. The command that will do the magic of passing messages is:
The above command will send the message, and also ring a bell on the PC speaker (see the -e switch used with echo and the \a character), for terminal tty1. The output will be like this: The \n will insert a newline character, just like printf.
Now, if you wish to send the message to multiple terminals, replace >/dev/tty1 with /dev/tty1>/dev/tty2>/dev/pts/1>rest/of/terminals. That’s it! Done!
Here’s a real world use of the above command combination. If you have users logged in through the the LAN via SSH, as root was in the above sample, and if you have to turn down the LAN interface of your system, send the message using
If you have KDE installed, you can even send a message to a user who has graphically logged in. KDE dutifully prints the message through ‘KWriteD’ to the screen, thus freeing you from the need of passing on the message to only those logged in through a command line. GNOME does not have a utility to do this, AFAIK. I tried it out on a Fedora 9 system with GNOME and it didn’t output any message to the screen.
That’s it for today! Have Fun!