
|
View Full Version : perl problems...
JohnDoe_Cro 07-28-2007, 06:38 PM hello all,
i am begginer in perl ...(know php)
now i try to run perl script
this code:
#!/usr/bin/perl
# Your first Perl Program to display message
# Program to display knowledge is power on screen
print "Knowledge is power \n";
and i get 500 internal server error...
does anyone have idea why?
chmod it to 755
trying to put it in cgi-bin and root.. same problems... i am doing something wrong.. but can someone help me and tell me what? thank you!
Steve_Arm 07-28-2007, 06:47 PM http://www.thesitewizard.com/archive/servererror.shtml
mwatkins 07-28-2007, 06:49 PM When debugging such things always confirm that the script runs from the command line, ideally as the user who will run it.
The link the previous poster provided is a good one. To that I'd also add that you are running CGI script but you aren't setting a header. If that isn't biting you now, eventually it will.
To see a live example, look at this script I just posted in another thread here:
http://www.webhostingtalk.com/showthread.php?t=623741
The relevant bit (python, but same principle):
def page(html):
print "Content-type: text/html"
print
print '<html>\n<head><title>%s Python configuration</title></head>' % os.uname()[1]
print '<body>\n%s</body>\n</html>' % html
In other words.... you need to add some lines to your perl code:
print "Content-type: text/html\n";
print "\n";
JohnDoe_Cro 07-28-2007, 06:56 PM When debugging such things always confirm that the script runs from the command line, ideally as the user who will run it.
The link the previous poster provided is a good one. To that I'd also add that you are running CGI script but you aren't setting a header. If that isn't biting you now, eventually it will.
To see a live example, look at this script I just posted in another thread here:
http://www.webhostingtalk.com/showthread.php?t=623741
The relevant bit (python, but same principle):
def page(html):
print "Content-type: text/html"
print
print '<html>\n<head><title>%s Python configuration</title></head>' % os.uname()[1]
print '<body>\n%s</body>\n</html>' % html
In other words.... you need to add some lines to your perl code:
print "Content-type: text/html\n";
print "\n";
THANK YOU! This works :)
thank you very much!
bye!
HostSentry 07-28-2007, 07:35 PM #!/usr/bin/perl
# Your first Perl Program to display message
# Program to display knowledge is power on screen
print "Content-type: text/html\n\n";
print "Knowledge is power \n";
You just need to state what type of file you are sending.
JohnDoe_Cro 07-28-2007, 08:14 PM Thank you all! :)
Now the new question.. i have downloaded some code for showing disk space...
find the code here
http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html
this is the code
#!/usr/bin/perl
use strict;
use warnings;
use Filesys::DiskSpace;
# file system /home or /dev/sda5
my $dir = “/home”;
# get data for /home fs
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
# calculate free space in %
my $df_free = (($avail) / ($avail+$used)) * 100.0;
# display message
my $out = sprintf(“Disk space on $dir == %0.2fn”,$df_free);
print "Content-type: text/html\n";
print "\n";
print $out;
but when runing this code there comes 500 internal server error...
could it be this:
# file system /home or /dev/sda5
my $dir = “/home”;
and what should i put instead of that?
on the server root is /dev/sda7
and i am not runing the script in root, i am runing the script on package www.nesto.com... so should I put something instead /home or is the error somewhere else?
i am sorry for these stupid questions but i need first to see how to fix errors so i can work alone and understand the basics of perl :)
Thank you for you help!
foobic 07-28-2007, 10:51 PM For development purposes only (not for production) add this line after "use warnings;"
use CGI::Carp qw(fatalsToBrowser);
It will give you more informative error messages to the browser.
Most likely the problem is that you didn't install the Filesys::DiskSpace module. If you have root install it using cpan, if not ask your host.
JohnDoe_Cro 07-29-2007, 07:48 AM Using that i get:
Unrecognized character \x93 at perl.pl line 9.
... but now i have fixed that (the problems was ") ok...
but now i get error Cannot use df on this machine (untested or unsupported). at perl.pl line 12
line 12:
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
why i can't use df and what should i tell my admins to do about that error message (should they install some new modul or what)?
Thank you very much!
foobic 07-29-2007, 08:46 AM Cannot use df on this machine (untested or unsupported). at perl.pl line 12
That would be an error message from the module itself. It means what it says. Is this a Windows system?
JohnDoe_Cro 07-29-2007, 08:48 AM No.. it's Linux CentOS...
What module? This -> Filesys::DiskSpace; or some else module?
And what should i do to fix that problem?
Thank you!
foobic 07-29-2007, 09:15 AM Yes, from the Filesys::DiskSpace module.
http://search.cpan.org/search?query=filesys%3A%3Adiskspace&mode=all
Looks like it's an old, unmaintained module. If you're curious you could try digging into the source code - you'll find it in
/usr/lib/perl5/site_perl/5.8.8/Filesys/DiskSpace.pm or somewhere similar.
Or maybe try Filesys::Df (http://search.cpan.org/~iguthrie/Filesys-Df-0.92/Df.pm) instead.
JohnDoe_Cro 07-29-2007, 09:30 AM everything (about modules) looks fine to me... i told my admins to take a look so i will update you =)
thank you
JohnDoe_Cro 07-29-2007, 01:18 PM My admins say that is an error in perl script...
can some one try this script #!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use Filesys::DiskSpace;
# file system /home or /dev/sda5
my $dir = "/home";
# get data for /home fs
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
# calculate free space in %
my $df_free = (($avail) / ($avail+$used)) * 100.0;
# display message
my $out = sprintf("Disk space on $dir == %0.2fn",$df_free);
print "Content-type: text/html\n";
print "\n";
print "eko";
and tell me does it work?
thank you
HostSentry 07-29-2007, 02:01 PM My admins say that is an error in perl script...
can some one try this script #!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use Filesys::DiskSpace;
# file system /home or /dev/sda5
my $dir = "/home";
# get data for /home fs
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
# calculate free space in %
my $df_free = (($avail) / ($avail+$used)) * 100.0;
# display message
my $out = sprintf("Disk space on $dir == %0.2fn",$df_free);
print "Content-type: text/html\n";
print "\n";
print "eko";
and tell me does it work?
thank you
The CPAN module couldn't install it so I'm not going to do it manually.
Try this though and see if you get an error:
#!/usr/bin/perl
use Filesys::DiskSpace;
my $dir = "/home";
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
my $df_free = (($avail) / ($avail+$used)) * 100.0;
print "Content-type: text/html\n\n";
print $df_free;
Also make sure you have read permissions for the /home directory. You might not. If it is a cpanel box you do though ;)
JohnDoe_Cro 07-29-2007, 02:25 PM that also does not work.. i get 500 internal server error...
but maybe it's the problem that i am not runing the script from root of the server i am runing it as user (from hosting account on server) ... could that be a problem?
thank you!
foobic 07-29-2007, 06:25 PM You should be able to run it as user. That module fails some tests on install so shouldn't be relied on, but the script did work for me on Fedora, after changing all the fancy quotes to regular ".
As expected, "Cannot use df on this machine (untested or unsupported)." is an error message from the module:
# grep "Cannot use" /usr/lib/perl5/site_perl/5.8.8/Filesys/DiskSpace.pm
|| Carp::croak "Cannot use df on this machine (untested or unsupported).";so if you get that your script is ok up to that point at least, but the module is telling you it won't work on your system.
And since the documentation (http://search.cpan.org/~ftassin/Filesys-DiskSpace-0.05/lib/Filesys/DiskSpace.pm) says:
Tested with Perl 5.003 under these systems :
- Solaris 2.[4/5]
- SunOS 4.1.[2/3/4]
- HP-UX 9.05, 10.[1/20] (see below)
- OSF1 3.2, 4.0
- Linux 2.0.*, 2.2.*
I'd say it's long out of date and shouldn't be used anyway.
JohnDoe_Cro 07-29-2007, 06:33 PM I see.... is there some other way to show with perl the status of disk space?
And in which languages were builded cPanel, Plesk etc...
I am gathering info because I am interested how that stuff works, and I would like to build some little script for my need that would show me disk space etc... (for fun)
Thank you!
foobic 07-29-2007, 07:03 PM For learning / for fun you could start with something like this (using the bash command df):
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
# file system /home or /dev/sda5
my $dir = "/home";
# get data for /home fs
#my $df = `df $dir`;
# or to get data for all mounted fs
my $df = `df`;
# do some processing here,
# eg. split the line into values, calculate %age free
# display message - replace with display of processed data
my $out = "Raw df output is:\n$df";
print "Content-type: text/plain\n\n";
print $out;
HostSentry 07-29-2007, 09:48 PM Maybe this will work for you.
#!/usr/bin/perl
use strict;
sub makeeasy{
my $size = shift;
$size *= 1024;
if($size > 1024){if($size > 1048576){if($size > 1073741824){$size = (int(($size / 1073741824) * 100) / 100) . ' GB';}else{$size = (int(($size / 1048576) * 100) / 100) . ' MB';}}else{$size = (int(($size / 1024) * 100) / 100) . ' KB';}}else{$size = $size . ' B';}
return $size;
}
my (%used,%available);
my $dir = "/home";
my $df = `df`;
my $otherused = 0;
my $otheravailable = 0;
my($actualpath,$totalspace,$usedspace,$availablespace,$usedpercent,$mountedon,$line);
while($df =~ m/\ \ /){$df =~ s/\ \ / /g;}
foreach $line (split("\\n",$df)){
($actualpath,$totalspace,$usedspace,$availablespace,$usedpercent,$mountedon) = split(' ',$line);
if($mountedon =~ m/^\//){
$used{$mountedon} = $usedspace;
$available{$mountedon} = $availablespace;
}
}
foreach $mountedon (keys(%used)){
print "Used space on $mountedon\: " . makeeasy($used{$mountedon}) ."\n";
print "Available space on $mountedon\: " . makeeasy($available{$mountedon}) ."\n";
}
JohnDoe_Cro 07-30-2007, 06:00 AM thank you all for your help! :)
|