Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing
Hello guys, this is my script :
Code:
#!/bin/sh Code:
mpstat 1 1 | grep "Priemer" | grep -m 1 "0,00" | awk '{print $3}' 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 |
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 Code:
value="`$MPSTAT 1 1 | grep "Priemer" | grep -m 1 "0,00" | awk '{print $3}'`"; 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. |
I changed it to this, but still, from console works, but from cron not, should I put that export somewhere else ?
Code:
#!/bin/sh |
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 |
Quote:
Code:
/usr/bin/mysql -u graph -p123 -D graph -e "INSERT INTO cpu (time, date, value) VALUES ('$time', '$date', '$value');" Code:
echo $time >> /tmp/myscript.$$.log |
By cron :
Code:
adam-server:~# adam-server:/tmp# ls Code:
./graph.sh |
Or, somebody know any command which directly echo/return average value of all cores in % ?
|
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. |
Code:
#!/bin/bash -xv |
All times are GMT -5. The time now is 07:43 PM. |