CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Finding Disk Usage

 



HR
Novice

Jan 20, 2001, 2:52 PM

Post #1 of 7 (495 views)
Finding Disk Usage Can't Post

I am trying to write a simple script to list the amount of disk space used and the amount of free disk space available. Please take a look at my code and tell me if I am basically going about it the right way. Am getting premature end of script headers errors and can't quite figure why.

#!/usr/local/bin/perl

########
# Define Variables

$basedir = "/mnt/web/guide/mywebsite";

# Done
########

&diskuse;

# Diskuse subroutine

sub diskuse {

opendir (DISK, $basedir);
@diskuse = du (DISK);
@diskfree = df (DISK);
closedir (DISK);

print "Content-type: text/html\n\n";
print "@diskuse";
print "<p>";
print "@diskfree";
}



Jasmine
Administrator / Moderator

Jan 20, 2001, 8:41 PM

Post #2 of 7 (488 views)
Re: Finding Disk Usage [In reply to] Can't Post

The "premature end of script headers" simply means that the print "Content-type: text/html\n\n"; line is missing. I'm assuming this is a program that's invoked by the browser?



sleuth
Enthusiast

Jan 20, 2001, 8:45 PM

Post #3 of 7 (487 views)
Re: Finding Disk Usage [In reply to] Can't Post

 
I dunno why, but when I move the print "Content-type: text/html\n\n"; up below the first line, the script gives no error, as it is i get the same error, so that's how I was able to at least get the thing running, but the code doesn't work at all in my tests and I don't have any suggestions there.

Tony



sleuth
Enthusiast

Jan 20, 2001, 8:52 PM

Post #4 of 7 (485 views)
Re: Finding Disk Usage [In reply to] Can't Post

 I just thought of something, since your only trying to measure one directory, if you loop through all the files in the directory you should be able to measure the size of the entire directory not including sub folders.

$dir = "/path/to/dir";
opendir (DIR, $dir);
@files =
sort
grep { -f "$dir/$_" }
readdir (DIR);
closedir (DIR);
foreach $file (@files)
{
$total_size .= -s "$dir$file";
}
print "The directory $dir is $total_size bytes in total.";

Tony



HR
Novice

Jan 20, 2001, 9:54 PM

Post #5 of 7 (480 views)
Re: Finding Disk Usage [In reply to] Can't Post

Yes, program is to be run in broswer which would save numerous steps from logging in via telnet and typing commands.

Premature end of script headers error is eliminated by moving the content-type line further up as suggested but my code is not returning any results. :o(

Also, this is to run in my root directory to tell how much of my space is being used and is free for my entire site. Log in to telnet and use du and df commands to see what I want to do. It is tedious to log in to telnet every time I need to check, set up logging of the session, etc so it would be a real timesave for me and anyone else who wants to see how much space is left on their site if this could be figured out.



sleuth
Enthusiast

Jan 20, 2001, 11:29 PM

Post #6 of 7 (478 views)
Re: Finding Disk Usage [In reply to] Can't Post

 
Ok, I see,

Well, if it works in telnet it should work like this,

Instead of

opendir (DISK, $basedir);
@diskuse = du (DISK);
@diskfree = df (DISK);
closedir (DISK);

Try

@diskuse = `du -m $basedir`;
@diskfree = `df -m $basedir`;

I added the -m switch for megabytes.

Tony



HR
Novice

Jan 22, 2001, 9:39 PM

Post #7 of 7 (465 views)
Nope, no dice with that [In reply to] Can't Post

but this does work:

#!/usr/local/bin/perl
system("df -k /mnt/web/guide/website>/mnt/web/guide/website/location/diskuse.txt");

print "Location:http://www.website.com/location/diskuse.txt\n\n";



 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives