KomaSatomi
asked on
Return value in script?
I use this script to return 0 when cpu load>80% and 1(256) when cpu<80%. And i call it in a C program like this:
sample.c
{
value_return=system("sh /home/dell/cpuload.sh");
printf ("value_return=%d",value_r eturn)
}
And i run sample.c (maybe few times a second):
cpu>80%: 0 0 0 0 0 0...
cpu<80% 256 256 0 256 0 256 256... <-- Anyone know why?
sample.c
{
value_return=system("sh /home/dell/cpuload.sh");
printf ("value_return=%d",value_r
}
And i run sample.c (maybe few times a second):
cpu>80%: 0 0 0 0 0 0...
cpu<80% 256 256 0 256 0 256 256... <-- Anyone know why?
#!/bin/sh
#/home/dell/cpuload.sh
LAST=""
STAT=`cat /proc/stat | grep cpu `
if [ -f /tmp/cpu_last ]; then
LAST=`cat /tmp/cpu_last `
fi
awk -v b="$LAST" -v a="$STAT" '
BEGIN {
if ( b == "" ) {
print "0";
print a > "/tmp/cpu_last";
exit;
}
split(a,data_a," ");
split(b,data_b," ");
d_u = data_a[2] - data_b[2];
d_n = data_a[3] - data_b[3];
d_s = data_a[4] - data_b[4];
d_i = data_a[5] - data_b[5];
load = 100 - (d_i/(d_u + d_s + d_n + d_i)*100);
print a > "/tmp/cpu_last";
if ( load >= 80 ) {
exit 0;
} else {
exit 1;
}
exit;}'
The return value of 'system' is implementation defined. Usually, it returns 0 when the call completed successfully, and non-zero otherwise.
If you are on a UNIX-like system, then you could use WEXITSTATUS(value_return) to get the real exit code of the command.
So :
int value_return = system("sh /home/dell/cpuload.sh");
if (WIFEXITED(value_return)) {
printf("value_return=%d", WEXITSTATUS(value_return));
}
else {
printf("system call failed !");
exit(1);
}
ASKER
I use Linux (Ubuntu). Can i use that command?Could you please show me the completely command?
>> I use Linux (Ubuntu). Can i use that command?
Yes.
>> Could you please show me the completely command?
I did ;)
Yes.
>> Could you please show me the completely command?
I did ;)
ASKER
Sorry.It display
warning: implicit declaration of function WEXITSTATUS
which lib i dont add yet?
IF my program C (so big) cant add any lib, is there other way to fix that problem?
warning: implicit declaration of function WEXITSTATUS
which lib i dont add yet?
IF my program C (so big) cant add any lib, is there other way to fix that problem?
WEXITSTATUS should be in :
#include <sys/wait.h>
#include <sys/wait.h>
ASKER
ok. i added that line and it display:
1 1 0 1 0 1 0...
Please check again.
I can open new question if you want.
1 1 0 1 0 1 0...
Please check again.
I can open new question if you want.
>> Please check again.
Euhm. Isn't that what you wanted it to display ?
Euhm. Isn't that what you wanted it to display ?
ASKER
No. i want it to display 1 1 1 1 1 1 in normal load (cpu<80%) and 0 0 0 0 0 in overload mode. sorry if i said not clearly.
ASKER
Or 256 256 256 256 256... in normal load. (not display 0 in normal load)
Ah, I see. I thought your question was about the 256 vs. 1.
I still don't see the problem though. It's not surprising that a CPU might go over 80% load very briefly, even when the system is not "in use". The script itself could be the cause.
I still don't see the problem though. It's not surprising that a CPU might go over 80% load very briefly, even when the system is not "in use". The script itself could be the cause.
ASKER
You mean the script has a problem? And there is no way to fix that?
I mean that it is normal that your CPU is used to its full potential. Even when a system is "idle", there are still certain tasks that need to be done (in the background), and they might take more than 80% of the CPU.
If you want to avoid the effect of small peaks, take averages over a certain time period (like a second for example), and use the averages to determine the load on the CPU.
If you want to avoid the effect of small peaks, take averages over a certain time period (like a second for example), and use the averages to determine the load on the CPU.
ASKER
I got it. can you give me more little help?^^
How take averages? I can do it in script? It really too difficult to me.
How take averages? I can do it in script? It really too difficult to me.
You are already sampling the stat data, and comparing it with the previous sample. You just have to make sure that the delay between two samples is sufficiently large, like 1 second for example.
ASKER
You mean sleep 1;where i can add it?
But i have to call it maybe few times a second.So what i have to do?
But i have to call it maybe few times a second.So what i have to do?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
^^. It's hard to do as you said. Thank anyway.