LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-24-2009, 09:23 AM   #1
adamlucansky
LQ Newbie
 
Registered: Sep 2009
Distribution: Distro Mr. Gates
Posts: 19

Rep: Reputation: 0
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
 
Old 09-24-2009, 09:34 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,006
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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.
 
Old 09-24-2009, 09:46 AM   #3
adamlucansky
LQ Newbie
 
Registered: Sep 2009
Distribution: Distro Mr. Gates
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 09-24-2009, 09:54 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 09-24-2009, 10:01 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by adamlucansky View Post
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
 
Old 09-24-2009, 10:04 AM   #6
adamlucansky
LQ Newbie
 
Registered: Sep 2009
Distribution: Distro Mr. Gates
Posts: 19

Original Poster
Rep: Reputation: 0
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

Last edited by adamlucansky; 09-24-2009 at 10:09 AM.
 
Old 09-24-2009, 11:51 AM   #7
adamlucansky
LQ Newbie
 
Registered: Sep 2009
Distribution: Distro Mr. Gates
Posts: 19

Original Poster
Rep: Reputation: 0
Or, somebody know any command which directly echo/return average value of all cores in % ?
 
Old 09-24-2009, 09:01 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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.
 
Old 09-25-2009, 07:26 AM   #9
adamlucansky
LQ Newbie
 
Registered: Sep 2009
Distribution: Distro Mr. Gates
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
LXer: How and When "Average Joe" and "Geek" Get New PCs (Comic) LXer Syndicated Linux News 2 05-25-2008 03:58 PM
Stupid question: if [ "$i" == `$(cat ${LOGFILESSHD} | grep "${i}" )` ] ; then frenchn00b Programming 6 05-19-2008 05:16 PM
bash equivalence of tcsh "alias em "emacs \!:1 &""? rgiggs Slackware 3 07-29-2004 02:07 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM


All times are GMT -5. The time now is 11:25 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration