Error while running a script through cron job


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Error while running a script through cron job
# 1  
Old 03-17-2014
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.
Code:
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:

Code:
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:

Code:
 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.

Moderator's Comments:
Mod Comment edit by bakunin: please use the CODE-tags which i inserted for you this time yourself from now on, as required by the rules. Thank you.

Last edited by bakunin; 03-17-2014 at 12:46 PM..
# 2  
Old 03-17-2014
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:
Code:
export PATH=/usr/sbin:/usr/bin:/sbin:/bin:$PATH

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

I hope this helps.

bakunin
# 3  
Old 03-17-2014
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
# 4  
Old 03-17-2014
What OS?
# 5  
Old 03-17-2014
Please set the xtrace option at the top of the script and post the execution log.
# 6  
Old 03-17-2014
Hi have added the option

set -o xtrace.

But i am still getting the same output.
# 7  
Old 03-17-2014
Instead of defining PATH, I would suggest using absolute path for all utilities located other than under /bin
Code:
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CRON job still running?

Hi All, I am writing a script that will eventually be executed by a cron job every 15 minutes. I want to make sure that my logic/script doesn't get executed if a previous job is still running. What would be the best way to handle that scenario? I was thinking to make my script create a temporary... (1 Reply)
Discussion started by: rdogadin
1 Replies

2. Solaris

Cron job running even after cron is removed

Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And... (4 Replies)
Discussion started by: chidori
4 Replies

3. Solaris

Cron job is not running

Hi, I have set up the crontab as follows. root@IDC4VASAPP07 # crontab -l 0-59 * * * * /var/tmp/r.sh 0-59 * * * * date >> /var/tmp/log root@IDC4VASAPP07 # r.sh is as follows. root@IDC4VASAPP07 # cat r.sh #!/bin/bash dt1=$(perl -e 'use POSIX;print strftime... (10 Replies)
Discussion started by: SunilB2011
10 Replies

4. Solaris

Shell Script gives error when run through cron job.

Hi, The following shell script runs without any problem when executed manulally. USED=$(df -h /arch | tail -1 | awk '{print $5}' | cut -d '%' -f 1) if then find /arch/AUBUAT/ -type f -mtime +0 | xargs rm find /arch/AUBMIG/ -type f -mtime +0 | xargs rm fi But the same gives below... (6 Replies)
Discussion started by: ksadiq79
6 Replies

5. Shell Programming and Scripting

Running script file using cron job every 5 second

Hi All, I beginner in unix, i have no idea how to set the script file using cron job every 5 second. I also want to execute automatically the output to text file.This is my script name countsys.sh and my textfile abc.txt. (6 Replies)
Discussion started by: mastercar
6 Replies

6. UNIX for Dummies Questions & Answers

Cron job not running

Hi All, I am editing crontab using -e option to add a new job Below is the line 30 * * * * scriptpath This job is not executing every thirty minutes. I have checked, cron daemon is running. What did I miss? Can some one help? I am using cron shell..ksh (7 Replies)
Discussion started by: yabhi_22
7 Replies

7. Shell Programming and Scripting

URGENT: cron job not running the sqlplus command in shell script

cron job not running the sqlplus command in shell script but the shell script works fine from command line.. Cronjob: 5 * * * * /home/dreg/script.sh script.sh: #!/bin/ksh /oracle/u000/app/oracle/product/10204/GEN/bin/sqlplus -s <user>/<pass>@<sid/home/dreg/sqlscript.sh ... (18 Replies)
Discussion started by: Ikea
18 Replies

8. Shell Programming and Scripting

Cron job giving error while running SSH command

Hi All, The script which i am using to SSH to remote server is working fine when i run is using ./ but when cron runs it it gives error that "ssh: not found" please help!!! (3 Replies)
Discussion started by: visingha
3 Replies

9. UNIX for Advanced & Expert Users

cron job is not running

hi, i have the following line in the crontab 15 5 * * 6 /home/adw/BCE_ADW.pl The problem is the cron job is not getting started automatically. But this was working til last week. now it is not working. what could be the problem. Any idea? (3 Replies)
Discussion started by: Suguna
3 Replies

10. HP-UX

Cron Job Not Running

Hi, I have a cron schedule like this 04,16,28,40,52 * * * * /nag/startProcessABatch (unix script) i want to add new lines in this file (like Logging), i just copy this file into a /tmp folder (for backup copy), and i have edited this file (added few lines of code for logging). ... (1 Reply)
Discussion started by: nag_sundaram
1 Replies
Login or Register to Ask a Question