Search Ali's Blog

Thursday, July 08, 2010

SMS notification for Linux commands

Sometimes you need to run a Linux command that may take time (such as kernel compilation, virus scan, long test run..) instead of waiting in front of the monitor, you can do other stuff (e.g., good coffee) and you'll be notified when the command had finished with the results.

I have ATT cell phone, they support SMS2EMAIL service; simply send a message (plain text, no longer than 160 characters) to the address:
(your-number)@txt.att.net
For example:
4081002000@txt.att.net.


The Email address varies based on your carrier, check this website for worldwide cellular network list.

Now, we just need to run the command that takes time, and send a short text message with the report to notify us.. from a Linux machine with Email access, run this command:
(msg-body) | mail -s (msg-subject) (sms2email-address)


The message body can be the command output, or the command returned code, or anything else you want.

Examples:
a) To report the returned code of an application, run:
gcc windows.c; echo "rc=$?" | mail -s "gcc report" 4081002000@txt.att.net
This will send you the returned code (rc) of the compilation command when its done, usually zero (rc=0) indicates a successful run.


b) To compile a Linux kernel and send an SMS when done with the output tail (last 120 bytes for example), run:
make 2>&1 | tail -c120 | mail -s "Kernel Compilation Status" 4081002000@txt.att.net

Notes:
- You may need to adjust 'tail -c' flag to fit into 160 characters, see 'man tail' for more info.
- To redirect command error messages, use the suitable bash redirection methods.
- ATT sucks.

No comments: