Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
, 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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.