LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script to display memory/cpu usage periodically (https://www.linuxquestions.org/questions/programming-9/script-to-display-memory-cpu-usage-periodically-4175668552/)

linxbee 01-27-2020 06:43 PM

script to display memory/cpu usage periodically
 
Hi, I am a newbie here.
How do we display the memory and cpu usage continuously on Linux target, I can do cat /proc/meminfo , or free and similar commands, but how can I run them like for every 1sec/ 2sec etc.

I don't have support of watch command.

BW-userx 01-27-2020 07:21 PM

top, or do a while script,
just a showing of what can be done.
Code:

#!/usr/bin/env bash

while sleep 2
do
        echo "Mem free"
        awk '/MemFree:/ {print $2 $3}' /proc/meminfo
        echo "MemAvailable:"
        awk '/MemAvailable:/ {print $2 $3}' /proc/meminfo
done

I am sure there are other things out there that give this info, I do not really worry about it so, yeah, there are others out there I am sure that can give you more info.

Depending on what desktop / window manager too, most all have there own plugins for these things.

linxbee 01-28-2020 02:49 AM

Quote:

Originally Posted by BW-userx (Post 6083627)
top, or do a while script,
just a showing of what can be done.
Code:

#!/usr/bin/env bash

while sleep 2
do
        echo "Mem free"
        awk '/MemFree:/ {print $2 $3}' /proc/meminfo
        echo "MemAvailable:"
        awk '/MemAvailable:/ {print $2 $3}' /proc/meminfo
done

I am sure there are other things out there that give this info, I do not really worry about it so, yeah, there are others out there I am sure that can give you more info.

Depending on what desktop / window manager too, most all have there own plugins for these things.


sleep utility is disabled for my target, its not in my hand to implement sleep utility, can it be done without sleep?

linom 01-28-2020 02:57 AM

Quote:

Originally Posted by linxbee (Post 6083690)
sleep utility is disabled for my target, its not in my hand to implement sleep utility, can it be done without sleep?

Try with cron job to be executed every 1/2 sec and redirect the output to a file. If that work then further enhancement can be done.

linxbee 01-28-2020 04:03 AM

Quote:

Originally Posted by linom (Post 6083691)
Try with cron job to be executed every 1/2 sec and redirect the output to a file. If that work then further enhancement can be done.

I am working on a embedded machine which has very limited set of utilities enabled, even cron not enabled !!!!

may be I will have to ask for sleep utility, if its cannot be done other way..

linom 01-28-2020 04:16 AM

Quote:

Originally Posted by linxbee (Post 6083706)
I am working on a embedded machine which has very limited set of utilities enabled, even cron not enabled !!!!

may be I will have to ask for sleep utility, if its cannot be done other way..

Can you use free command with parameters as below:

Quote:

free -s 1
Continuously display the result delay seconds apart. You may actually specify any floating point number for delay

NevemTeve 01-28-2020 05:37 AM

Maybe:
Code:

#!/bin/sh
exec /usr/bin/top


BW-userx 01-28-2020 06:44 AM

Looks like you do not have many tools to work with,
there is this little trick that is done in programming by running a loop with an astronomical (ball park) number to get the loop to count up to.

This is using a loop to create a wait state before moving on. The operator/programmer just needs to work out the "magic" number needed to get the loop to count to in order to get the desired time limit needed.
Code:

#!/usr/bin/env bash

while true
do
        for (( i=0;$i<9999;i++))
        do
                ((c++)) #give it something to do
                #when it hits the magic number it will
                #exit loop preform operations below
                #then start over doing this again
        done
        echo "Mem free"
        awk '/MemFree:/ {print $2 $3}' /proc/meminfo
        echo "MemAvailable:"
        awk '/MemAvailable:/ {print $2 $3}' /proc/meminfo
        c=0 #this has no need other then to give the
            #the for loop something to do while it
            #is counting up to x amount in the (( ))
done


Geist 01-28-2020 07:06 AM

Is ping enabled?

If you have ping then you can ping localhost (or maybe even 0.0.0.0 not sure) for a "sleep" replacement.
Default time between pings is one second, and if you wanted to wait five seconds, then you'd do six pings (since the first is 'immediate'), like this:
Code:

ping -c 6 127.0.0.1
If you wanted to wait 10 seconds:

Code:

ping -c 11 127.0.0.1
So the count, on default settings otherwise, is seconds + 1.

individual 01-28-2020 07:14 AM

Since you're apparently working with a very stripped down set of tools, it would be helpful to know what commands you do have access to.

EDIT: There are several alternatives to wait without the sleep command listed in this Stack Exchange thread.

BW-userx 01-28-2020 07:40 AM

I just happnen to be in FreeBSD now, but yeah 'at'

Code:

[userx@FreeBSD ~]$ echo "HIeoolo at me boy" | at 07:38
at: you do not have permission to use this program

[userx@FreeBSD ~]$ sudo echo "HIeoolo at me boy" | at 07:38
at: you do not have permission to use this program

[userx@FreeBSD ~]$ whereis at
at: /usr/bin/at /usr/share/man/man1/at.1.gz /usr/src/usr.bin/at

[userx@FreeBSD ~]$ su
Password:

[root@FreeBSD /home/userx]# echo "HIeoolo at me boy" | at 07:45
Job 1 will be executed using /bin/sh
[root@FreeBSD /home/userx]#

root login might too be needed in Linux to use at. Then there is the repeat 'at' to be worked out to make 'at' fire off every x amount until shut down.

Which looks to be problematic. 'at' would have to be put into a loop then that loop got to have a means to be put to sleep or something before it calls 'at' again to set a new time to output information.

It does keep a log though.


All times are GMT -5. The time now is 07:15 AM.