LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing (https://www.linuxquestions.org/questions/linux-general-1/problem-%24value%3D%60mpstat-1-1-%7C-grep-average-%60%3B-alias-pipe-return-nothing-757491/)

adamlucansky 09-24-2009 09:23 AM

Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing
 
Hello guys, this is my script :

Code:

#!/bin/sh

databse="graph";
user="graph";
pass="123";
table="cpu";
value="`mpstat 1 1 | grep "Priemer" | grep -m 1 "0,00" | awk '{print $3}'`";
time=`date +%s`;
date=`date +"%Y-%m-%d %T"`;


mysql -u graph -p123 -D graph -e "INSERT INTO cpu (time, date, value) VALUES ('$time', '$date', '$value');"

My problem is, if i execute in console
Code:

mpstat 1 1 | grep "Priemer" | grep -m 1 "0,00" | awk '{print $3}'
, then it works fine, it return 0,99 . Fine for now, now, when I execute script ./graph.sh , it correctly write into mysql value 0,99. So now time to automatize, when i put it into cron (bcron), when it run script, it instead of "0,99" just "" , i have no idea where should be problem.

http://img87.imageshack.us/img87/7200/script.jpg

And I should not that if that $value is just "mpstat 1 1" without pipe it works fine

MensaWater 09-24-2009 09:34 AM

Cron doesn't inherit your user environment like you do when you login.

The most common problem in cron scripts is expectation of variables, especially PATH, that are populated by your login but are minimal in cron.

To solve this you can add a PATH= statement to your cron and insure the directories it includes are the ones that contain mpstat, awk, grep and any other command you're using in the file.

Alternatively you could put the full path to commands each time you use them or create variables for each full path to executable then use that variable in place of the command. e.g.
Code:

export MPSTAT=/usr/bin/mpstat
Then in command line:
Code:

value="`$MPSTAT 1 1 | grep "Priemer" | grep -m 1 "0,00" | awk '{print $3}'`";
Of course for that command line in that approach you'd want variables for grep and awk as well). You'd also need to create one for other commands used int he script such as mysql.

If there are other variables mysql or other commands rely upon from your login you'd want to include them in the script as well. PATH is just the most common one.

adamlucansky 09-24-2009 09:46 AM

I changed it to this, but still, from console works, but from cron not, should I put that export somewhere else ?

Code:

#!/bin/sh

export MPSTAT=/usr/bin/mpstat
export GREP=/bin/grep
export AWK=/usr/bin/awk

databse="graph";
user="graph";
pass="123";
table="cpu";
value=`$MPSTAT 1 1 | $GREP "Priemer" | $GREP -m 1 "0,00" | $AWK '{print $3}'`;
time=`date +%s`;
date=`date +"%Y-%m-%d %T"`;

mysql -u graph -p123 -D graph -e "INSERT INTO cpu (time, date, value) VALUES ('$time', '$date', '$value');"

echo $time
echo $date
echo $value


catkin 09-24-2009 09:54 AM

A quick-and-dirty method of simulating your entire logged-on environment, including $PATH, is to change the first line of the script to
Code:

#!/bin/bash -l
That presumes you are using bash as your login shell, not sh. The -l (letter l) tells bash to "make this shell act as if it had been directly invoked by login". See GNU Bash Reference.

catkin 09-24-2009 10:01 AM

Quote:

Originally Posted by adamlucansky (Post 3695593)
I changed it to this, but still, from console works, but from cron not, should I put that export somewhere else ?

Code:

#!/bin/sh

export MPSTAT=/usr/bin/mpstat
export GREP=/bin/grep
export AWK=/usr/bin/awk

databse="graph";
user="graph";
pass="123";
table="cpu";
value=`$MPSTAT 1 1 | $GREP "Priemer" | $GREP -m 1 "0,00" | $AWK '{print $3}'`;
time=`date +%s`;
date=`date +"%Y-%m-%d %T"`;

mysql -u graph -p123 -D graph -e "INSERT INTO cpu (time, date, value) VALUES ('$time', '$date', '$value');"

echo $time
echo $date
echo $value


mysql needs to be treated in the same way as mpstat etc. or you could simply change it to (I'm guessing the location of mysql so please check it using type mysql at a command prompt)
Code:

/usr/bin/mysql -u graph -p123 -D graph -e "INSERT INTO cpu (time, date, value) VALUES ('$time', '$date', '$value');"
When the job is run by cron, there is no terminal to send stdout to so it will bork on the echo commands unless you redirect the entire script output to a file in the crontab or explicitly in the script itself with something like
Code:

echo $time >> /tmp/myscript.$$.log

adamlucansky 09-24-2009 10:04 AM

By cron :
Code:

adam-server:~# adam-server:/tmp# ls
graph.7209.log  graph.7231.log  mc-root
adam-server:/tmp# cat graph.7231.log
1253804881
2009-09-24 17:08:01

adam-server:/tmp#

In shell :
Code:

./graph.sh
1253804955
2009-09-24 17:09:15
0,99


adamlucansky 09-24-2009 11:51 AM

Or, somebody know any command which directly echo/return average value of all cores in % ?

chrism01 09-24-2009 09:01 PM

Try using

set -xv

as the 2nd line in the script, and capture all the stdout/stderr of the script in cron eg

1 2 * * * /home/me/myscript.sh >/home/me/myscript.log 2>&1

I'd also specify bash as the shell rather than sh, there are a few differences. Your option of course.

adamlucansky 09-25-2009 07:26 AM

Code:

#!/bin/bash -xv


databse="graph";
+ databse=graph
user="graph";
+ user=graph
pass="123";
+ pass=123
table="cpu";
+ table=cpu
value=`mpstat 1 1 | grep "Priemer" | grep -m 1 "0,00" | awk '{print $3}'`;
mpstat 1 1 | grep "Priemer" | grep -m 1 "0,00" | awk '{print $3}'
++ mpstat 1 1
++ grep Priemer
++ grep -m 1 0,00
++ awk '{print $3}'
+ value=
#value=`/home/scripting/cpu.sh`;
time=`date +%s`;
date +%s
++ date +%s
+ time=1253881501
date=`date +"%Y-%m-%d %T"`;
date +"%Y-%m-%d %T"
++ date '+%Y-%m-%d %T'
+ date='2009-09-25 14:25:01'

export MPSTAT=/usr/bin/mpstat
+ export MPSTAT=/usr/bin/mpstat
+ MPSTAT=/usr/bin/mpstat
export GREP=/usr/bin/mpstat
+ export GREP=/usr/bin/mpstat
+ GREP=/usr/bin/mpstat
export AWK=/usr/bin/mpstat
+ export AWK=/usr/bin/mpstat
+ AWK=/usr/bin/mpstat

/usr/bin/mysql -u graph -p123 -D graph -e "INSERT INTO cpu (time, date, value) VALUES ('$time', '$date', '$value');"
+ /usr/bin/mysql -u graph -p123 -D graph -e 'INSERT INTO cpu (time, date, value) VALUES ('\''1253881501'\'', '\''2009-09-25 14:25:01'\'', '\'''\'');'

echo $time >> /tmp/graph.$$.log
+ echo 1253881501
echo $date >> /tmp/graph.$$.log
+ echo 2009-09-25 14:25:01
echo $value >> /tmp/graph.$$.log
+ echo

echo $time
+ echo 1253881501
1253881501
echo $date
+ echo 2009-09-25 14:25:01
2009-09-25 14:25:01
echo $value
+ echo



All times are GMT -5. The time now is 07:43 PM.