LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-22-2017, 02:06 AM   #1
krishnapa
LQ Newbie
 
Registered: Mar 2017
Posts: 12

Rep: Reputation: Disabled
Running script in cron giving different output


Hi,

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 ?

Thanks,
 
Old 03-22-2017, 02:52 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
Old 03-22-2017, 05:04 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I addition to the excellent advice above, I'd also recommend http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
Old 03-22-2017, 06:36 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
  


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
script output different when cron runs it aristosv Linux - General 12 06-30-2015 12:22 PM
[SOLVED] A script is not giving any output. Satyaveer Arya Programming 21 02-12-2012 08:36 PM
Running Command in Cron without giving away password DJOtaku Linux - Security 8 11-17-2009 07:08 AM
cron job did not redirect output from bash script junust Programming 2 07-26-2009 04:30 AM
how to silence script output (so I don't get so many cron logs)? BrianK Programming 2 03-01-2005 04:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:43 PM.

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