Running same script through cron gives different output

Hi All,

I am running the below shell script through cron and surprisingly it gives different output

$uname -a
Linux 2.6.18-194.3.1.7.3.el5xen #1 SMP Fri Jul 30 00:08:45 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
$ echo $SHELL
/bin/bash
shell script:

cat sar_cpu.sh

#!/bin/bash


hdr=1
while true; do
  sar -u 10 1 |  awk '/user/{if(hdr){print $1,$2,$4,$6,$7};next} /^[0-9]/{print $1,$2,$4,$6,$7}'  OFS=, hdr=$hdr >> /sar_cpu_`date +%m%d%y`.csv
  sleep 5
  hdr=0
done
Output when the script runs through cron:

$ cat sar_cpu_040913.csv

20:09:01,CPU,%nice,%iowait,%steal
20:09:11,all,0.00,0.05,0.00
20:09:22,all,0.00,0.03,0.00
20:09:33,all,0.00,0.05,0.03
20:09:44,all,0.00,0.03,0.00
20:09:55,all,0.00,0.03,0.00
20:10:06,all,0.00,0.03,0.00
20:10:17,all,0.00,0.03,0.03
20:10:28,all,0.00,0.03,0.00
20:10:39,all,0.00,0.03,0.00
20:10:50,all,0.00,0.03,0.00
20:11:01,all,0.00,0.25,0.02
20:11:12,all,0.00,0.00,0.00
20:11:23,all,0.00,0.02,0.00
20:11:34,all,0.00,0.03,0.03
20:11:45,all,0.00,0.02,0.00
20:11:56,all,0.00,0.05,0.00
20:12:07,all,0.00,0.05,0.00
20:12:18,all,0.00,0.05,0.03

I get the expected output when the script is run manually as below:

$ ./sar_cpu.sh &
or 
$ nohup ./sar_cpu.sh &

Expected output:

$ cat sar_cpu_040913.csv

05:35:27,PM,%user,%system,%iowait
05:35:37,PM,0.18,0.00,0.03
05:35:48,PM,0.20,0.00,0.15
05:35:59,PM,0.15,0.03,0.03
05:36:10,PM,1.13,1.68,0.55
05:36:22,PM,0.08,0.00,0.03
05:36:33,PM,0.10,0.03,0.00
05:36:44,PM,0.23,0.50,0.00
05:36:55,PM,0.33,0.03,0.03
05:37:06,PM,0.15,0.00,0.00

Any suggestions please as to what is going wrong while executing the same script through cron?

Thanks for your time!

Regards,
a1_win

For 24 hour format set LC_TIME to POSIX

eg:

LC_TIME="POSIX" sar -tu 5 1 |  awk ...

or for AM/PM

LC_TIME= sar -tu 5 1 |  awk ...

Hi Chubler_XL,

Will this help resolve in getting the expected output by running the sript through cron?

Thanks,
a1_win

yes it should do

Hi Chubler_XL,

The issue is not resolved after setting the code as below:

LC_TIME="POSIX" sar -tu 5 1 |  awk ...

It still gives the incorrect output while running the script through cron as metioned earlier in this post:

Output when the script runs through cron:

$ cat sar_cpu_040913.csv

20:09:01,CPU,%nice,%iowait,%steal
20:09:11,all,0.00,0.05,0.00
20:09:22,all,0.00,0.03,0.00
20:09:33,all,0.00,0.05,0.03
20:09:44,all,0.00,0.03,0.00
20:09:55,all,0.00,0.03,0.00
20:10:06,all,0.00,0.03,0.00
20:10:17,all,0.00,0.03,0.03
20:10:28,all,0.00,0.03,0.00
20:10:39,all,0.00,0.03,0.00
20:10:50,all,0.00,0.03,0.00
20:11:01,all,0.00,0.25,0.02
20:11:12,all,0.00,0.00,0.00
20:11:23,all,0.00,0.02,0.00
20:11:34,all,0.00,0.03,0.03
20:11:45,all,0.00,0.02,0.00
20:11:56,all,0.00,0.05,0.00
20:12:07,all,0.00,0.05,0.00
20:12:18,all,0.00,0.05,0.03

Expected output should be:

$ cat sar_cpu_040913.csv

05:35:27,PM,%user,%system,%iowait
05:35:37,PM,0.18,0.00,0.03
05:35:48,PM,0.20,0.00,0.15
05:35:59,PM,0.15,0.03,0.03
05:36:10,PM,1.13,1.68,0.55
05:36:22,PM,0.08,0.00,0.03
05:36:33,PM,0.10,0.03,0.00
05:36:44,PM,0.23,0.50,0.00
05:36:55,PM,0.33,0.03,0.03
05:37:06,PM,0.15,0.00,0.00

Any other suggestions please?

Thanks,
a1_win

Well you have something in your environment cron doesnt have...
What do you have in your env that could be time display related?

Your two outputs show totally different columns. I doubt they are created by the same, identical script, once run interactively, and once by cron.
As cron will deploy a very limited environment, sourcing your ~/.profile may help defining the environment you are used to use interactively, but make sure you source it from the absolute path.

I am not familiar with sar, but the observed discrepancy can be the result of a single missing field (field 2, the AM/PM field) shifting subsequent fields by -1.

The fields printed are 1, 2, 4, 6, 7. If the only difference is that the incorrect result's input is missing the AM/PM field, then its output translates to fields 1, 3, 5, 7, and 8 from the correct input. We don't know what 3, 5, and 8 look like. We do know that field 7 is iowait and it is shifted to the left by one as expected.

Again, I am not familiar with sar. I don't know what's going on. I'm just pointing out that this could be the result of column 2 being absent.

Some tools use different default formats for root than they do for unprivileged users. Perhaps this is one of them? Is the cronjob privileged while the manual runs are not?

Regards,
Alister

Hi Rudi C,

Thanks but even sourcing the .profile from absolute path didn't help resolve the issue when running the script through cron

I think there is something else that is missing when the script runs througn cron.

Whereas, manually running the same script gives the correct expected output

Any other suggestions please?

Thanks,
a1_win

alister made a very good point. Please modify the script to include all columns, and post output of manual and cron run. Do so by either getting rid of awk, or by printing $0 in awk. You may also want to print your locale settings for both runs.

Hi Alister/RudiC,

Thanks a lot for your time! Appreciate your help!

I was able to get the expected output after modifying the script and running through cron as below:

shell script:

cat sar_cpu.sh

#!/bin/bash


hdr=1
while true; do
  sar -u 10 1 |  awk '/user/{if(hdr){print $1,$3,$5,$6};next} /^[0-9]/{print $1,$3,$5,$6}'  OFS=, hdr=$hdr >> /sar_cpu_`date +%m%d%y`.csv
  sleep 5
  hdr=0
done

Regards,
a1_win