Tech & Work

Set administrative reminders in Linux with AT and MAIL

Learn how to use AT and MAIL for setting up reminders and alerts in Linux.


You're buried in projects and new initiatives, and you've been putting out fires left and right. How are you supposed to stay on top of all those mundane—but all-important—administrative responsibilities too? A couple of little applications in Linux, AT, and MAIL can help. While some tasks can be scheduled simply by using a CRON job, other tasks, such as reviewing sensitive log files, require manual intervention from the administrator. These are the kinds of tasks that are good candidates for using the one-two punch of AT and MAIL.

The AT command reads other commands from standard input or from a specified file and executes them at a designated time using /bin/sh. The MAIL command is an intelligent mail-processing system, which has a command syntax reminiscent of the text editor ED(1), with lines replaced by messages. I will walk you through the process of using these two commands to create e-mail reminders for your administrative tasks. This is a fairly low-tech—yet creative and flexible—way of scheduling important reminders.

Using AT
The AT command offers a simple way to run programs or commands at a specified time, although it can take on many complexities. The syntax of AT is:
at [-V] [-q queue] [-f file] [-mldcv] TIME

Table A lists the arguments included in this syntax.

Table A
at This is the command itself.
-V This outputs the version number.
-q This uses the specified queue.
-f This reads the job from file rather than standard input.
-m This sends mail to the user when the job has completed even if there was no output.
-l This is an alias for atq, which lists the user's pending jobs; for the superuser, all jobs are listed.  
-d This is an alias for atrm, which deletes jobs.
-c This does a CAT for the jobs listed on the command line to standard output.
-v For atq, this shows completed but not yet deleted jobs in the queue; otherwise, it shows the time the job will be executed.
TIME This is the user-specified time for the program/commands execution.

What the information in Table A (which is from the MAN page) neglects to mention is that the command itself is appended after the TIME section and can be simplified by using only the following syntax:
at TIME COMMAND

In other words, let's say we want a listing of the contents of our home directory stored at 5:00 P.M. For that task we would enter:
at 17:00

That command will respond with a > prompt. At the > prompt, enter the command to be issued, such as:
ls /home/USER > /home/USER/listing

After you enter the command, press [Ctrl]D to execute it. This particular command will have a file called listing created in your home directory at 5:00 P.M., and in it will be a listing of the contents of your home directory.

Using MAIL
The MAIL command is an antiquated system for delivering mail to specified users. Although MAIL is powerful, it has its limitations. However, even with those limitations, it comes in quite handy when used in conjunction with AT.

There is one small issue to overcome before using the MAIL command. Out of the box, MAIL will treat the from attribute (in the e-mail header) as localhost or some variation on your host name. To fix this problem, you will have to change the host name of the computer. Just open up a console, su to root, and run the following commands:
hostname your.computer.hostname
/etc/rc.d/init.d/network restart

Make sure that you restart your network after you change your host name, or you will get this error:
Xlib: connection to ":0.0" refused by server
Xlib: Client is not authorized to connect to server
aterm: can't open display :0.0

After you’ve executed the two commands above, you will want to edit your /etc/hosts file to reflect the host name you just added. Open /etc/hosts in your favorite editor and look at the line that has your machine's IP address. You should see something similar to this:
127.0.0.1       localhost.localdomain   localhost
172.22.1.2      domain.com              hostname

In place of domain.com, you will see the name you entered when you set up your Linux box. In place of hostname, you will see the host name you gave your machine. You need to replace domain.com with your network's domain name. With this change, MAIL will have the correct domain name after the @ symbol in its from address. After you’ve added this, restart your network (again run /etc/rc.d/init.d/network restart) to ensure that this change has taken effect.

Now that we have MAIL configured correctly, let's try it out. The basic syntax for MAIL is:
mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr]

The breakdown of this syntax appears in Table B.

Table B
mail This is the main command.
-i This ignores tty interrupt signals, which is particularly useful when using mail on noisy phone lines.
-I This forces mail to run in interactive mode even when input isn't from a terminal. In particular, the '~' special character when sending mail is active only in interactive mode.
-n This inhibits reading/etc/mail.rc upon startup.
-v This is for verbose mode. The details of delivery are displayed on the user's terminal.
-s subject This is the subject of the mail.
-c cc-addr This is the address where a carbon copy of mail will be sent.
-b bcc-addr This is the address where a blind carbon copy of mail will be sent.

Here's the simple method of using MAIL:
Mail target@name.com

After you type this, press [Enter], and you will see this:
Subject:

At this prompt, just enter the desired subject and press [Enter] again. You'll then see something like this:
Cc: [jwallen@giles jwallen]$ mail jwallen@techrepublic.com
Subject: This is my test of mail

You will notice the last line is blank. This is where your cursor will be and where you will enter the body of your message. Once you've typed the message, press [Enter] again, followed by [Ctrl]D, which will give you this:
cc:

Enter e-mail addresses of anyone you want to Cc and press [Enter] (or simply press [Enter] if you don't want to Cc the message to anyone). You'll be returned to the command prompt and your mail will be sent.

Putting it all together
Of course, such simple applications are going to have limitations. But all we're trying to do is develop a basic reminder system. The combination of AT and MAIL will give us a simple setup to use the subject line of an e-mail to provide our reminder—and there you run into a limitation. When combining the two commands (at least in this way), you can't enter text into the body of the e-mail, so the subject has to say it all.

To combine the two applications, you must run AT first using the following syntax:
at TIME

For example:
at now+2min

This will run the command two minutes from now. Once you run this command, you will find yourself staring at:
[jwallen@giles jwallen]$ at now+2min
at>

Here, you enter the application that's going to run at the given time. For our little reminder system, we'll enter something like this:
mail -s "Remember to do this" user@address.com

The text in italics will be displayed in the subject of the e-mail. The user@address.com is the destination e-mail address.

Now, press [Enter] and then [Ctrl]D, which will enter an end-of-file (EOF) marker that the application can recognize.

Final word
Yes, there are GUI applications on Linux, such as StarOffice, that have integrated mail schedulers and reminders. So what is the point of this AT and MAIL tip? It's illustrated how much latitude you have in Linux to compose simple and creative solutions to common (and sometimes not so common) problems. Showing you how to combine various applications in Linux highlights the power and flexibility of the Linux command-line tools, such as AT and MAIL, which can be used to send important reminders to your entire IT staff at the same time and to create other custom reminders that go beyond the limits of personal calendar applications.

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox