Error while running a script through cron job

Hi Team,

When i am running the below query manually it is giving me the right output
i.e.

export PATH=/usr/sbin:/usr/bin:/sbin:/bin:$PATH
ADMIN=abc@abc.com
CPU_HIGH=`sar|awk '{print $9}'|sort -n|head -5|sed -n 5p`
CPU_MAX=`echo "scale=3; 100-$CPU_HIGH" | bc`
CPU_LOW=`sar|awk '{print $9}'|sort -n|tail -1`
CPU_MIN=`echo "scale=3; 100-$CPU_LOW" | bc`
MEM_MAX=`cat /root/admin_files/server_monitor/datewise_monitor/ram_monitor-$(date +"%d-%B-%Y")|awk '{print $2'}|sort -n|tail -1`
MEM_MIN=`cat /root/admin_files/server_monitor/datewise_monitor/ram_monitor-$(date +"%d-%B-%Y")|awk '{print $2'}|sort -n|head -1`
now=`date +-%Y-%m-%d-%H:%M:%S`
file=utilization_$now.log
filename=/root/admin_files/server_monitor/datewise_monitor/$file
echo "$(date): Memory Utilization Report for server $(hostname) is" >>$filename
echo "-----------------------------------" >>$filename
echo "                                   " >>$filename
echo "CPU Max Utilization :  $CPU_MAX %  " >>$filename
echo "CPU Min Utilization  :  $CPU_MIN % " >>$filename
echo "                                   " >>$filename
echo "Max Ram Utilization :  $MEM_MAX %  " >>$filename
echo "Min Ram Utilization  :  $MEM_MIN % " >>$filename
cat $filename| mail -s "$(date +"%d-%B-%Y"): CPU & Memory Summarization Report for server $(hostname) : "$(ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}')" " $ADMIN
sar >> /root/admin_files/server_monitor/datewise_monitor/cpu_monitor$(date +"-%d-%B-%Y").txt
sar -r >>/root/admin_files/server_monitor/datewise_monitor/memory_monitor$(date +"-%d-%B-%Y").txt

Output:

Mon Mar 17 14:47:28 GMT 2014: Memory Utilization Report for server mmv0pfecdel0070 is
  -----------------------------------
                                     
  CPU Max Utilization :  7.29 %  
  CPU Min Utilization  :  .07 % 
                                     
  Max Ram Utilization :  12.000 %  
  Min Ram Utilization  :  11.800 % 

But when i am running the script through cronjob it is giving me the below output:

 Mon Mar 17 15:02:01 GMT 2014: Memory Utilization Report for server mmv0pfecdel0070 is
  -----------------------------------
                                     
  CPU Max Utilization :   %  
  CPU Min Utilization  :   % 
                                     
  Max Ram Utilization :  12.000 %  
  Min Ram Utilization  :  11.800 % 

Please assist me in this.

This seems to be an - albeit hidden - case of Cron Problem Number One. I suppose "sar" is not in the PATH, because all the other utilities you used are, as far as i could see.

Btw., it doesn't help you to add the old PATH:

export PATH=/usr/sbin:/usr/bin:/sbin:/bin:$PATH

because "$PATH" is empty when a cron-script starts.

I hope this helps.

bakunin

Even after adding the sar path in the export does not resolve the issue. I have removed $PATH from the end.

Still getting the same error. Please assist.

This is the entry in the cron tab

41 15 * * * /root/admin_files/server_monitor/server_monitor.sh

What OS?

Please set the xtrace option at the top of the script and post the execution log.

Hi have added the option

set -o xtrace.

But i am still getting the same output.

Instead of defining PATH, I would suggest using absolute path for all utilities located other than under /bin

CPU_MAX=`echo "scale=3; 100-$CPU_HIGH" | /usr/bin/bc`

This is just an example, you have to find the absolute path for each utilities using which command and replace.

You have to capture stderr in a file to see the log.

Please find the output

++ export PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/sar
++ PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/sar
++ ADMIN=abc@abc.com
+++ /usr/bin/sar
+++ /usr/bin/awk '{print $9}'
+++ /bin/sort -n
+++ /usr/bin/head -5
+++ /bin/sed -n 5p
++ CPU_HIGH=
+++ echo 'scale=3; 100-'
+++ /usr/bin/bc
(standard_in) 2: parse error
++ CPU_MAX=
+++ /usr/bin/sar
+++ awk '{print $9}'
+++ sort -n
+++ tail -1
++ CPU_LOW=
+++ echo 'scale=3; 100-'
+++ /usr/bin/bc
(standard_in) 2: parse error
++ CPU_MIN=
+++ awk '{print $2}'
++++ date +%d-%B-%Y
+++ sort -n
+++ tail -1
+++ cat /root/admin_files/server_monitor/datewise_monitor/ram_monitor-17-March-2014
++ MEM_MAX=12.200
+++ awk '{print $2}'
++++ date +%d-%B-%Y
+++ sort -n
+++ head -1
+++ cat /root/admin_files/server_monitor/datewise_monitor/ram_monitor-17-March-2014
++ MEM_MIN=11.800
+++ date +-%Y-%m-%d-%H:%M:%S
++ now=-2014-03-17-18:20:01
++ file=utilization_-2014-03-17-18:20:01.log
++ filename=/root/admin_files/server_monitor/datewise_monitor/utilization_-2014-03-17-18:20:01.log
+++ date
+++ hostname
++ echo 'Mon Mar 17 18:20:01 GMT 2014: Memory Utilization Report for server mmv0pfecdel0070 is'
++ echo -----------------------------------
++ echo '                                   '
++ echo 'CPU Max Utilization :   %  '
++ echo 'CPU Min Utilization  :   % '
++ echo '                                   '
++ echo 'Max Ram Utilization :  12.200 %  '
++ echo 'Min Ram Utilization  :  11.800 % '
++ cat /root/admin_files/server_monitor/datewise_monitor/utilization_-2014-03-17-18:20:01.log
+++ date +%d-%B-%Y
+++ hostname
+++ ifconfig eth0
+++ sed -n '/inet /{s/.*addr://;s/ .*//;p}'
++ mail -s '17-March-2014: CPU & Memory Summarization Report for server mmv0pfecdel0070 : 10.129.192.198 ' abc@abc.com
++ sar
+++ date +-%d-%B-%Y
++ sar -r
+++ date +-%d-%B-%Y

---------- Post updated at 01:45 PM ---------- Previous update was at 01:44 PM ----------

The same code is working when running manually.

---------- Post updated at 02:34 PM ---------- Previous update was at 01:45 PM ----------

i guess there is an issue when using bc in the crontab.

It is not the problem with bc .

The problem is sar output is different when running from cron and from command line.

From command line:

12:00:01 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM     all     51.22      0.01      6.16      0.23      0.00     42.37

From crontab:

00:00:01        CPU     %user     %nice   %system   %iowait    %steal     %idle
00:10:01        all     51.22      0.01      6.16      0.23      0.00     42.37

Set LC_TIME before running sar to display AM/PM data:

LC_TIME=en_US.UTF-8 sar

One question :

1) what if instead of awk'{print $9}' i try using awk'{print $8}'

2) Will adding the line
LC_TIME=en_US.UTF-8 in the beginning of the script will suffice??

Yes, you can try awk'{print $8}' instead. But then it will not work from command line.

So the best approach is to set LC_TIME and it will work either way.

so i need to add LC_TIME in the beginning of the script??

Yes

export LC_TIME=en_US.UTF-8