1834903 Members
3086 Online
110071 Solutions
New Discussion

Re: Script output.

 
Jeff Martin_3
Advisor

Script output.

This is part of a script I am trying to run from the cron. The script works fine manually but doesn't work from the cron. The output from the "if ping 149.197.238.180 -n 3 | grep '100% packet loss'" line doesn't get filtered by grep because it is running from cron. The output from the ping statement either gos to email or std out.

########################################################
if [ "$BB6509LC" -ne 1 ]
then
if ping 149.197.238.180 -n 3 | grep '100% packet loss'
then
telalertc -i JeffMartin -m "Critical Switch Unreachable: BB-6509-LANCENTER"
BB6509LC=1
fi
fi
########################################################
if [ "$BB6MSFCDSTBY" -ne 1 ]
then
if ping 149.197.238.180 -n 3 | grep '100% packet loss'
then
telalertc -i JeffMartin -m "Critical Switch Unreachable: BB-6MSFC-DASD-STBY"
BB6MSFCDSTBY=1
fi
fi
########################################################

How do I get around this so my script works properly?

Thanks,

Jeff Martin
5 REPLIES 5
curt larson_1
Honored Contributor

Re: Script output.

usually when this situation happens, i.e. scripts runs manually but not via cron, is because cron's environment is different from the users.

courtesy of bill hassell:

cron NEVER logs in like a normal user so nothing in /etc/profile or .profile is run. All cron jobs should set PATH explicitly (actually, all reliable scripts should do this) to avoid issues like this. And of course, specialized variables that may have been added to login profiles should also be coded into cron scripts. Use the set command in your cron job to show what your current environment contains.

Hai Nguyen_1
Honored Contributor

Re: Script output.

Jeff,

Can you try to enter the full path name for ping as well as for grep? Then retry.

Hai
David Lodge
Trusted Contributor

Re: Script output.

Chances are is that /usr/sbin isn't on the PATH. For some *strange* reason somebody in the wide world of SYSV decided that ping should live in /usr/sbin instead of /usr/bin...

The solution is to explicitly use /usr/sbin/ping.

BTW with your script you will get rubbish emailed to you; you can suppress the output of grep by doing:

...
if /usr/sbin/ping 149.197.238.180 -n 3 | grep -q '100% packet loss'
then
...

dave
Darren Prior
Honored Contributor

Re: Script output.

Hi,

You'll also need the full path of the telalertc command too.

It would also be wise to have a #!/usr/bin/sh at the top of the script too (if you haven't already!)

regards,

Darren.
Calm down. It's only ones and zeros...
Renda Skandier
Frequent Advisor

Re: Script output.

Hi Jeff,
I do this pretty often... just redireting the output of the ping to a temp file, then grep the temp

>/tmp/isp.test
/usr/etc/ping -s64 -c1 $isp >/tmp/isp.test

if [ `cat /tmp/isp.test | grep -c "1 packets received"` = 0 ]
then