troubleshooting Question

Return value in script?

Avatar of KomaSatomi
KomaSatomi asked on
LinuxLinux OS DevC++
19 Comments1 Solution1114 ViewsLast Modified:
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_return)
}
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;}'
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 19 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 19 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros