Friday, September 30, 2011

Send email in linux

In new walk-through environment, there are some configuration issues need look at application logs to see what is going on. SSH to application server (tomcat) and vi the application log is the common way. Ops team can alsomail the log file to other developers to analyze, so need sendmail. Luckily there is "sendmail" in Linux.

Command Line:

  1. Check if sendmail is running => ps ax | grep sendmail
  2. Check if sendmail is running => /etc/init.d/sendmail status
  3. Check sendmail listening info (IP and port) => netstat -tnlp | grep sendmail
  4. Send email (please use man mail for user guide)
    1. mail -s "Email subject" example@example.com < /tmp/test.txt
    2. cat /tmp/test.txt | mail -s "Email subject" example@example.com
    3. echo "The message in email body" | mail -s "Email subject" example@example.com
    4. (df -h;free -m) | mail -s "Disk and Memory Info" example@example.com
    5. less /proc/cpuinfo | mail -s "CPU Info" example@example.com -c ccto@example.com
Shell script:
#!/bin/sbin
#df -h | mail -s "disk space" example@example.com
#!/bin/bash
df -h > /tmp/mail_report.log
free -m >> /tmp/mail_report.log
mail -s "disk and RAM report" example@example.com < /tmp/mail_report.log
rm -f /tmp/mail_report.log

No comments:

Post a Comment