LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-14-2010, 02:24 PM   #1
makowka
Member
 
Registered: Feb 2007
Posts: 42

Rep: Reputation: 8
Small bash script to notify server user


Hi

I want to share a small script, which checks, whether a server (e.g. FTP-server) is working and sends to the server user(s) a notification email ("Server is UP"). After the server has been shut down, the script sends another message to the user(s): "Server is DOWN".

Code:
#!/bin/sh

CHECKINGPERIOD=10                  # in sec

if [ -n "$(pidof proftpd)" ]
   then
   /usr/bin/mail -s 'FTP is UP' user@isp.com -- -r me@isp.com
   else
   exit 0
fi

while [ 1=1 ];

do
    if [ -z "$(pidof proftpd)" ]
       then
       /usr/bin/mail -s 'FTP is DOWN' user@isp.com -- -r me@isp.com
       exit
    fi

sleep $CHECKINGPERIOD

done
This script needs the exim/postfix smtp server (configured as smarthost), in order to work. It can be started after the ftp server has been started MANUALLY with the command "nohup /usr/local/bin/ftpcheck > /dev/null 2>&1". The scripts ends, as soon as it discovers, that the server isn't up anymore. That's all, I know, that this is not very complicated code, but it took me some time to write it in a clean way. I hope, it will be of use for someone. All suggestions are welcome.
 
Old 02-14-2010, 03:16 PM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
As I understand your script, it will check once to see if the server is up.
If so it sends email and then continues running, only to send you email every
10 seconds when the server is down.

That means if the ftp server goes down in the middle of the night, you'll be
bombarded with email in the morning. Also you'll never be notified when it
comes back up.
 
Old 02-15-2010, 02:17 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by makowka View Post
Code:
while [ 1=1 ];
Thanks for sharing

The above snippet is not a test because the "=" test operator must have whitespace around it. The cleanest way to set up an infinite loop is
Code:
while true
or, less legibly
Code:
while :
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Small part of Bash Script that I would like to explain it to me jefn Programming 10 04-11-2009 04:09 AM
Very small bash script enemorales Programming 8 02-25-2005 02:47 AM
small bash script issue zoomzoom Linux - General 7 06-08-2004 06:33 PM
small bash script to kill a PID flosch Linux - General 3 05-06-2004 09:28 PM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration