Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to prepare health checks for some servers where I need disk space and telnet status. So I have prepared below script -
=========================
#! /bin/bash
for j in server1 server2
do
echo "In $j Server -"
`exit`
ssh -q $j "df -h | grep /dev" | while read output;
do
usep=$(echo $output | awk '{ print $5}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $1,$2,$3,$4,$5,$6}' )
if [ $usep -ge 90 ]; then
echo "Running out of Disk Space "$partition ($usep%)" PLEASE CHECK"
fi
done
echo ""
(echo quit) | telnet $j 8001 > /tmp/Disk/a
cat /tmp/Disk/a | grep "Connected to" > /tmp/Disk/a.log
if [ -s /tmp/Disk/a.log ];
then
echo "Port 8001 is listening and service is UP"
else
echo "Port 8001 is not listening and service is DOWN"
fi
done
echo "$(cat /tmp/Disk/Monitoring.log)" | mail -s "Status of SERVERS" "abc@xyz.com"
======================================
So when I run the above script with -
/tmp/Disk/Monitoring.sh > /tmp/Disk/Monitoring.log command it gives me email as -
================================
In server1 -
Running out of Disk Space "/dev/cciss/c0d0p3 3.9G 3.4G 390M 90% /usr (90%)" PLEASE CHECK
Port 8001 is listening and service is UP
------------------------------------------------------------------------------------------------
In server2 -
Port 8001 is listening and service is UP
================================
But when I try with cron -
42 06 * * * /tmp/Disk/Monitoring.sh > /tmp/Disk/Monitoring.log
It gives me mail output as -
===================================
In server1 -
Port 8001 is listening and service is UP
------------------------------------------------------------------------------------------------
In server2 -
Port 8001 is listening and service is UP
------------------------------------------------------------------------------------------------
==================================
No disk space output it shows? i.e.
Running out of Disk Space "/dev/cciss/c0d0p3 3.9G 3.4G 390M 90% /usr (90%)" PLEASE CHECK
What needs to be change ?
It will help greatly if you post your code between [code] [/code] tags so that the white space is preserved.
Speaking of white space, you have an extra space after the #!, not that it should matter.
However, in the cron environent, PATH is not set the same as you may be expecting, so it is a good idea to either set it at the beginning of your script or to use absolute path names throughout the script.
The cut is unnessary with awk.
The function tempfile is useful for creating temporary files and might be considered here.
In debugging your script you can add "set -xv" just after the #!/bin/bash line (and I'd remove the space there as Turbocapitalist has identified)
By turning on verbose debugging you'll see the output of the commands more and be able to diagnose your script better and see what is happening with your df command.
You can check the result of commands like the ssh and you should for now to understand if it passed or failed, because not seeing output there indicates that the loop does not run, or does not do what you expect it to do.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.