I would like to use a bash script (python would be second best) to monitor regularly (hourly) if my mailserver is online and operating.

I know that there are dedicated solutions for this task (Nagios, ...) but I really need something simple that I can use as a cronjob. Only to see the mailserver is alive.

I know how to talk with a mailserver with telnet, ie:

telnet mail.foo.org 25
EHLO example.com
mail from:
rcpt to:

but this is interactive. Is it possible to check with a script that the mailserver is communicating? Obviously, I don't want to go the whole way and actually send an email. I just want to test that the mailserver is responding.

  • Is checking if the server is enough? Does the mailserver can crash alone? – A.L Apr 19 '14 at 16:00
  • Check out the code snippet in my answer here – Bananguin Apr 19 '14 at 16:27
  • A system management tool like chef or puppet would be a good tool to make sure it stays running. – spuder Apr 20 '14 at 1:59

You can use nc to test a SMTP mail server like so:

$ nc -w 5 mail.mydom.com 25 << EOF
HELO mail.mydom.com

NOTE: The options -w 5 tell nc to wait at most 5 seconds. The server to monitor is mail.mydom.com and 25 is the port we're connecting to.

You can also use this form of the above if you find your server is having issues with the HELO:

$ echo "QUIT" | nc -w 5 mail.mydom.com 25

NOTE: This form works well with both Postfix and Sendmail!


Here I'm connecting to my mail server.

$ echo "QUIT" | nc -w 5 mail.bubba.net 25
220 bubba.net ESMTP Sendmail 8.14.3/8.14.3; Sat, 19 Apr 2014 16:31:44 -0400
221 2.0.0 bubba.net closing connection

If you check the status returned by this operation:

$ echo $?

However if nothing at the other ends accepts our connection:

$ echo QUIT | nc -w5 localhost 25
Ncat: Connection refused.

Checking the status returned from this:

$ echo $?

Putting it together

Here's my version of a script called mail_chkr.bash.


echo "Checking Mail Server #1"
echo "QUIT" | nc -w 5 mail.bubba.net 25 > /dev/null 2>&1

if [ $? == 0 ]; then
  echo "mail server #1 is UP"
  echo "mail server #1 is DOWN"

echo "Checking Mail Server #2"
echo "QUIT" | nc -w 5 localhost 25 > /dev/null 2>&1

if [ $? == 0 ]; then
  echo "mail server #2 is UP"
  echo "mail server #2 is DOWN"

Running it:

$ ./mail_chkr.bash 
Checking Mail Server #1
mail server #1 is UP
Checking Mail Server #2
Ncat: Connection refused.
mail server #2 is DOWN
  • I think it's cleaner to send a single . to terminate the SMTP session, rather than EOF after the HELO. – Bananguin Apr 19 '14 at 18:56
  • @Bananguin - actually when I tried that it gave me this error: 500 5.5.1 Command unrecognized: ".". Adding a QUIT however worked, so I'll add that. – slm Apr 19 '14 at 19:00
  • Works great. I have added -w 5 for nc as a timeout of 5 sec. Otherwise it seems to hang indefinitely when it cannot reach the server. – user1968963 Apr 19 '14 at 19:40
  • @user1968963 - Yes I meant to mention that. I'll add it into the answer. – slm Apr 19 '14 at 19:40
  • @slm - on the mailserver, I see these messages in the log: postfix/smtpd improper command pipelining after HELO from unknown: QUIT\n. Can we get rid of them, or is postfix just complaining that I hang up prematurely? – user1968963 Apr 19 '14 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.