Operating System - Linux
1827967 Members
2561 Online
109973 Solutions
New Discussion

Re: Strange shell script behaviour

 
kholikt
Super Advisor

Strange shell script behaviour

I am writing some script to monitor CPU usage of the remote host using SSH. If the CPU is less than 10% idle it will send out an alert via email.

Apparently my script only work if I run it in foreground but not cron. It seem to be something to do with the remote ssh execution, the output was not captured.

PATH=/usr/bin:/opt/openssh/bin:
export PATH
rm -f /tmp/sarcheck
ssh username@hostname "sar -u 30 5" > /tmp/sarcheck
IDLE=`cat /tmp/sarcheck | grep Average | awk '{print $5}'`
function func_send_email
{
(
echo "To:myemail@my.com"
echo "Subject:CPU Alert idle"
) | /usr/sbin/sendmail -t
}
echo $IDLE >> /tmp/cpu.log
if [ $IDLE -lt "10" ]
then
echo "Warning High CPU more than 90%"
func_send_email
fi

please advise
abc
4 REPLIES 4
Pete Randall
Outstanding Contributor

Re: Strange shell script behaviour

The first issue I see is with your PATH. You only have /usr/bin and sar lives in /usr/sbin. You need to specify full path names of all commands in a cron script or explicitly define a full PATH variable.


Pete

Pete
Carlos Roberto Schimidt
Regular Advisor

Re: Strange shell script behaviour

Hi,

For looking problems with scrits running by cron, try redirect errors messages e default output:

0,15,30,45 * * * * /your_script.sh >/tmp/output.txt 2>/errors.txt

You can put "set -x" in the begin of the script for debug.

Remember when you submit script for cron, is possible see this output:

"warning: commands will be executed using /usr/bin/sh"

Sometimes a script run well in korn shell, but not run so well in posix shell.

Schimidt
kholikt
Super Advisor

Re: Strange shell script behaviour

The error come out you have no controlling tty

+ rm -f /tmp/sarcheck
+ ssh user@hostname sar -u 30 5
+ 1> /tmp/sarcheck
You have no controlling tty. Cannot read passphrase.
+ + cat /tmp/sarcheck
+ grep Average
+ awk {print $5}
IDLE=
+ echo
+ 1>> /tmp/cpu.log
+ [ -lt 10 ]
abc
Senthil Kumar .A_1
Honored Contributor

Re: Strange shell script behaviour

Hi,


Can you try "ssh" with "-T" option.

This is what I read related to "-T" option...

"-T Disable pseudo-tty allocation."

Regards,
Senthil Kumar .A
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)