LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2020, 06:43 PM   #1
linxbee
Member
 
Registered: Jan 2020
Distribution: RHEL,CENTOS, Ubuntu
Posts: 52

Rep: Reputation: Disabled
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.
 
Old 01-27-2020, 07:21 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 01-27-2020 at 07:26 PM.
 
1 members found this post helpful.
Old 01-28-2020, 02:49 AM   #3
linxbee
Member
 
Registered: Jan 2020
Distribution: RHEL,CENTOS, Ubuntu
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
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?
 
Old 01-28-2020, 02:57 AM   #4
linom
Member
 
Registered: May 2015
Location: India
Distribution: Debian, CentOS,Redhat, Fedora, Ubuntu
Posts: 91

Rep: Reputation: 13
Quote:
Originally Posted by linxbee View Post
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.

Last edited by linom; 01-28-2020 at 02:59 AM.
 
Old 01-28-2020, 04:03 AM   #5
linxbee
Member
 
Registered: Jan 2020
Distribution: RHEL,CENTOS, Ubuntu
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linom View Post
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..
 
Old 01-28-2020, 04:16 AM   #6
linom
Member
 
Registered: May 2015
Location: India
Distribution: Debian, CentOS,Redhat, Fedora, Ubuntu
Posts: 91

Rep: Reputation: 13
Quote:
Originally Posted by linxbee View Post
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

Last edited by linom; 01-28-2020 at 04:17 AM.
 
Old 01-28-2020, 05:37 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Maybe:
Code:
#!/bin/sh
exec /usr/bin/top
 
Old 01-28-2020, 06:44 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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

Last edited by BW-userx; 01-28-2020 at 06:54 AM.
 
Old 01-28-2020, 07:06 AM   #9
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
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.
 
Old 01-28-2020, 07:14 AM   #10
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
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.

Last edited by individual; 01-28-2020 at 07:20 AM.
 
1 members found this post helpful.
Old 01-28-2020, 07:40 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

Last edited by BW-userx; 01-28-2020 at 07:46 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for CPU usage, memory usage, disk partition space and service status reetesh.amity Linux - Server 6 10-12-2015 07:51 PM
Get total cpu usage, total memory usage and available memory Chiba Linux - Software 1 11-15-2014 04:36 PM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
Controlling CPU usage & memory usage Saravana babu Linux - Software 0 02-18-2004 05:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration