Software

Send messages and alerts from the command line

Vincent Danen introduces two command-line programs, wall and mail, that allow you to send simple alerts and e-mail messages to users.

The command line is a great environment for doing many things. It can be used to schedule commands, navigate directories, chain simple commands together to create complex commands, automate system monitoring, and more. The flexibility that the command line offers makes it worth investing the time into learning more about it, as its power far exceeds that of GUI operating environments.

CLI environments, especially with multi-user systems such as servers, provide a great way to communicate between users as well. Two tools in particular are of great value: wall, which will communicate messages on all active consoles, and mail, which will send e-mail via the command-line.

Wall is an interconsole "instant messenger" and is available on all Linux and UNIX systems. With it, you can broadcast alert messages, such as an impending system reboot or other maintenance process. Using wall is simple:

$ wall "just fooling around"
Broadcast message from vdanen (pts/16) (Mon Aug  4 20:47:03 2008):
just fooling around

Any user logged into the system will see the identical notice. The only exception is when a user has set their "message" flag off. For instance, to disable seeing these wall notices, use:

$ mesg n
$ mesg
is n

The mesg command takes exactly one of two arguments: y and n; if no argument is supplied, it shows the current message value.

Another useful tool is the mail command, which will send e-mail messages on the CLI. This can be used in conjunction with a command such as at to send reminder e-mails, or it can be used to send an alert when a task or command is complete. There are a few different providers of the mail command; some distributions use the mailx package, others use nail, others the original Berkely mail package. If mail doesn't work as expected, read the mail(1) manpage. Most mail commands use the same syntax for the basics, however. To send an e-mail to an SMS relay to alert you of an upcoming meeting, use:

$ echo "20 minutes to meet with Sally." | /bin/mail -s "Meeting" sms@myhost.com

This will send an e-mail with the subject "Meeting" to sms@myhost.com; the body of the message will contain, "20 minutes to meet with Sally." As has been illustrated, this can be used as a quick means of sending reminders, or it can be used to send the output of jobs when completed. The mail command takes standard input as the body of the message; other options can be used to specify CC and BCC recipients as well.

E-mail messages sent via mail are sent from the calling user on the system's hostname; for instance, if the user account joe were sending this on the system foo.bar.org, then the e-mail would be seen to come from joe@foo.bar.org. Because of this, you may want to ensure proper message rules are in place that permit the relaying or reception of these addresses, or adjust the hostname of the computer accordingly.

Get the PDF version of this tip here.

Delivered each Tuesday, TechRepublic's free Linux and Open Source newsletter provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

9 comments
Photogenic Memory
Photogenic Memory

Like this: ]# telnet 127.0.0.1 25 Trying 127.0.0.1... Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. 220 localhost.localdomain ESMTP Sendmail 8.13.8/8.13.8; Tue, 2 Sep 2008 19:39:22 -0700 ehlo crabbypatties.com 250-localhost.localdomain Hello localhost.localdomain [127.0.0.1], pleased to meet you 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-8BITMIME 250-SIZE 250-DSN 250-ETRN 250-DELIVERBY 250 HELP mail from:spongebob@crabbypatties.com 250 2.1.0 spongebob@crabbypatties.com... Sender ok rcpt to:patrick@starfish.net 250 2.1.5 patrick@starfish.net... Recipient ok data 354 Enter mail, end with "." on a line by itself Whew! It's a good thing that was only a sea bear, this dirt circle would never protect us against a sea rhinoserous. It's a good thing I'm wearing my anti-sea rhinoserous undergarments. . 250 2.0.0 m832dMDn008646 Message accepted for delivery noop 250 2.0.0 OK

asistemas
asistemas

Interesting but what happens if I run the mail comand say from my home computer Does it have it's own SMTP engine or I need to add a mail server for it to work. Also, if the computer name is say "sakura" will the email be sent from username@sakura (just that?)

Demo_Dog
Demo_Dog

BLAT does this after a fashion.

martian
martian

Now why would anyone want to use something like Telnet which has been proven to be insecure?

gwainwright
gwainwright

One of my favorite Sponge Bob episodes, right up there with Pizza Delivery...

Dr Dij
Dr Dij

run a mail server on their home PC, tho you can. You need to use your ISPs server if at home or work server if at work. I used commail.exe from dos prompt and configured it for our work server. I then had a prog create dos .bat files as needed that sent out email notifications that run had completed at end of file processing runs. Read parameters (who to send email to, etc) from a file.

option12
option12

On a linux machine, the odds are good that a smtp server is running, or at the least installed and just needs to be turned on. Try the command above and if it doesn't work, check your distribution docs. The possibility does exist that your ISP will block outbound mail from an smtp server on your network, so you may need to configure your setup to relay though your ISP.

option12
option12

oh, and yes without further configuration the from email address will be "user@hostname", so if you want it to appear to come from a different email address, their will be some configuration involved