Script runs manually but not correctly from crontab

Hello all,
I'm new here and have a question if you don't mind helping me. I have a script that will work if I kick if off manually but not from Cron. My cron entry is this:

05,20,35,50 * * * * /scripts/status.sh > /dev/null 2>&1

The first script (works fine) is this:

#!/bin/sh
#
# ****************************************************************************************
# Purpose: Script to check the status of the following:
#       Disk usage - Call mon_fs.sh (this is dependent on mon_fs.dat 
#       Memory Usage - memorywatcher.sh
#       MWTM Server process - ServerUporDown.sh (this is dependent on /NOT.txt file 
# Notifies via e-mail.
# Usage: Execute from crontab every 15 minutes.
# Outputs: None - if one of the child scripts detects and error it will email the group 
# ****************************************************************************************
# The directory this script resides in
ADMINDIR=/scripts
# remove old status.log file
rm /scripts/status.log
# Fist 2 scripts to monitor disks and memory
/scripts/mon_fs.sh
# section to run the server status command
/opt/CSCOsgm/bin/mwtm status >> /scripts/status.log
# script to determine if any of the server process are down.
/scripts/ServerUporDown.sh 

At the end of that script it call another one (the broken one) called ServerUporDown.sh. This is the one that is not working.

#!/bin/ksh
# ****************************************************
# Porpose: Montior Status of server components
# Notifies on trigger via eamil
# Usage: Execute from Crontab every 15 miniuites
# Outputs: email
# ***************************************************
#
# The directory this script resides in
ADMINDIR=/scripts
#
# The next variable can be set for multiple addresses
# (i.e. jsmith@yahoo.com,jsmith@hotmail.com)
MAILADD=abc@mail.com
# Define the hostname of the server
SRVNM=`uname -n`
# Remove old file 
rm /scripts/NOT.txt
# location of error messages
more /scripts/status.log | grep NOT >> /scripts/NOT.txt
# Logic to check if anything was written to the NOT file
# and email group if error was found
if [ ! -s "/scripts/NOT.txt" ]
then
echo "No error found."
else
echo "Sending eamil"
mailx -s $SRVNM" Server down - Action required" $MAILADD < NOT.txt
fi
exit 0

I'm sure it something simple... everything about it work fine but won't email that result when there is a entry into the NOT.txt file. It updates the status.log file and the NOT.txt file according the time stamps as it should.

Thoughts?

mailx -s $SRVNM" Server down - Action required" $MAILADD < /scripts/NOT.txt

methyl - Thanks... staring at it too long and being up for 20+ hours... need sleep.. should have seen that. After making that change... all still the same.... everything works up to the point of email me

The untrapped output from the script should be in the unix mail file for the owner of the cron.
Check whether the "Sending eamil" (sic) message appears in that mailbox and whether there are any other error messages.
You might want to redirect the cron output to a logfile and not to /dev/null while you are testing.

I'd certainly move this quote to encompass the whole subject line but whether it causes trouble depends on the value of $SRVNM.

mailx -s "$SRVNM Server down - Action required" $MAILADD < /scripts/NOT.txt

This line is dodgy but might work.
Would be much better without the pointless "more" as:

grep "NOT" /scripts/status.log  >> /scripts/NOT.txt

I'd also replace

more /scripts/status.log | grep NOT >> /scripts/NOT.txt

with

grep 'NOT' /scripts/status.log >> /scripts/NOT.txt

EDIT: methyl was faster...

After you could try this alternative (you have now your /scripts/NOT.txt using methyl's or cero's suggestion):

\tail -1  /scripts/NOT.txt | $ux2dos | \mailx -s "$SRVNM Server down - Action required" $MAILADD

$ux2dos is for path and name of your unix2dos program (I set it as variable because many scripts I wrote run on different platforms...)

ok - script now looks like this:

#!/bin/ksh
# ****************************************************
# Porpose: Montior Status of server components
# Notifies on trigger via eamil
# Usage: Execute from Crontab every 15 miniuites
# Outputs: email
# ***************************************************
#
# The directory this script resides in
ADMINDIR=/scripts
#
# The next variable can be set for multiple addresses
# (i.e. jsmith@yahoo.com,jsmith@hotmail.com)
MAILADD=abc@abc.com
# Define the hostname of the server
SRVNM=`uname -n`
# Remove old file 
rm /scripts/NOT.txt
# location of error messages
grep "NOT" /scripts/status.log >> /scripts/NOT.txt
# Logic to check if anything was written to the NOT file
# and email group if error was found
if [ ! -s "/scripts/NOT.txt" ]
then
echo "No error found."
else
echo "Sending eamil"
mailx -s "$SRVNM Server down - Action required" $MAILADD < /scripts/NOT.txt
fi
exit 0

crontab looks like this:

05,20,35,50 * * * * /scripts/status.sh >> /scripts/results.txt

output of results.txt looks like this:

Sending eamil
Sending eamil 

It has gone thru 2 cycles. Looks like a mail problem but I more in the dark now... when I cat a file and mailx it to myself it works (cat /etc/hosts | mailx abc@abc.com) , this script works when I force it with ./ServerUporDown.sh

:wall:

Check your mail logs to see if it sent an email ... and to where ... and that it is not "deferred" or something.

Did you look at the unix mail for the user who owns the cron? If there is such an email it should contain the error.

any ideas on how to look at the mail as it goes out?

---------- Post updated at 10:58 AM ---------- Previous update was at 10:46 AM ----------

not sure how to do that

You have not tried yet my suggestion by the look of things...

@hs3082
There is much variation in unix mail. What Operating System and version is this?

5.10 Generic_142909-17 sun4v sparc SUNW,Netra-T5440

---------- Post updated at 11:16 AM ---------- Previous update was at 11:15 AM ----------

VBE - I don't see your suggestion... I'm sorry I missed it.

Post #7
Adapt to your needs ( tail - value...can be replace by more, cat...)

ok --- black box.... it has started working... strange... thanks for your suggestions.