LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-19-2008, 05:44 PM   #1
kanik
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Rep: Reputation: 0
Displaying free disk space and memory


Hey, I need to display the free disk space in the /tmp folder and the free memory of the computer.
I'm using the following commands right now:

df -h /tmp
free -t -m

These give me a column of info like:

Filesystem Size Used Avail Use% Mounted on
tmpfs 2.0G 188K 2.0G 1% /tmp

But I only need to display the avail memory. I tried using the cut function to do so but wasn't able to do it.

So how do I do this? It can be done in the terminal or as a part of a C++ code.

Cheers
--Kanik
 
Old 08-19-2008, 06:11 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
free -t -m reports:
Code:
bash-3.1$ free -t -m
             total       used       free     shared    buffers     cached
Mem:           438        432          6          0        263         84
-/+ buffers/cache:         83        355
Swap:          172          2        170
Total:         611        434        176
bash-3.1$
so a simple parse would display the free memory:
Code:
bash-3.1$ free -t -m | egrep Mem | awk '{print $4}'
6
bash-3.1$
Unless I misunderstand what it was you were looking for...
 
Old 08-19-2008, 07:38 PM   #3
kanik
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Thats exactly what I was looking for. Thanks a lot!!
 
Old 08-19-2008, 07:43 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
If that's all you're looking for, might be easier to grep /proc/meminfo
 
Old 08-19-2008, 10:23 PM   #5
kanik
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by syg00 View Post
If that's all you're looking for, might be easier to grep /proc/meminfo
Cheers, I'll try that method as well.

I'm also having a problem with converting the obtained memory/free disk space to string. I'm using this code:

PHP Code:
string DiskSensor::readSensorValue()
  {
string diskspace;  
diskspace system("df /tmp | egrep tmpfs | awk '{print $4}'");
return 
diskspace;
  }
// Return a name for our sensor
string DiskSensor::getSensorTitle()
  {
  return 
"Free Disk Space";
  }


// Return the value read by the sensor
string DiskSensor::getSensorValue()
  {
  return 
readSensorValue();
  } 
And in another file linked to this:
PHP Code:
  cout << "Sensor Title is: " << disksensor.getSensorTitle() << endl;
  
cout << "Sensor Value is: " << disksensor.getSensorValue() << endl
And I get this as output:

PHP Code:
Sensor Title isFree Disk Space
2096984
Sensor Value is

When I want :
PHP Code:
Sensor Title isFree Disk Space
Sensor Value is
:2096984 kB 
So how can I fix the string and how do I add a "kB" at the end of the disk space?

Cheers
--Kanik

Last edited by kanik; 08-20-2008 at 01:35 AM.
 
Old 08-20-2008, 11:35 AM   #6
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Using the system() call is rarely necessary...

For example, the disk space info can be obtained directly using statfs()
And the free memory info is available via getrusage() and/or getrlimit()

hth
 
Old 08-20-2008, 12:16 PM   #7
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
I second that. Use C calls (preferably standard POSIX calls) when available to system() calls with shell parameters. It is more portable.
 
Old 08-20-2008, 10:29 PM   #8
kanik
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
I can't seem to get the getrusage command working. I'm more familiar with the system command so I would rather do it that way. So is this the right way to make the output from system command to a string and return it?

PHP Code:
string diskspace;  
diskspace system("df /tmp | egrep tmpfs | awk '{print $4}'");
return 
diskspace
Thanks
 
Old 08-21-2008, 11:15 AM   #9
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Nope, that won't work.

system() only returns the exit status of the shell command, meaning it will only tell you whether the command succeeded or not. The output of the command will not be captured.

There are several ways to do that, but again, it is more work than it is worth.
 
Old 08-23-2008, 10:59 PM   #10
kanik
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Ok. Is there a way to put the output from the system(...) command into a text file? cuz if I can do that I know the rest...

Last edited by kanik; 08-24-2008 at 04:24 AM.
 
Old 08-24-2008, 10:22 PM   #11
kanik
LQ Newbie
 
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Nevermind, I figured it out. Just added a ">abc.txt" at the end.
Thanks for the help!
 
  


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
How t get it known free disk space? ukrainet Linux - General 3 05-06-2008 10:52 PM
free() does not clear memory space in LINUX xinminhua Programming 32 08-18-2006 03:43 PM
Linked list free memory space at the end cdog Programming 12 04-17-2006 08:25 AM
rm doesn't free the disk space krizzz Linux - General 5 02-16-2005 11:27 AM
Do I have enough disk space and memory? MarkFour Linux - Newbie 2 02-06-2004 07:43 PM

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

All times are GMT -5. The time now is 03:59 PM.

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