image ResEx Logo
ResExcellence www : Powered by Google
Cell Phone Themes Icons Mighty Mouse Cursors Software Reviews Widgets & Widgets


Files are in Stuffit 5 or greater format.
Free download.

Tell us about a bad link.

Thank You!


Running 55 days
without a restart.

Scripting with Perl

Today's tidbit improves on the earlier example of printing the amount of time the computer has been running.. Here are some problems I solved in this latest version.

We want the output to print, "2 days", or "47 minutes", but what if the server has been booted for one minute or one day? So there is an if (then) else test to see if the time_units should be singular or plural.

The format of "uptime" is different for the 1st hour through to the 23rd hour. Another loop tests for this condition and formats the output accordingly.

Running 55 days without a restart!
Running <!--#exec cgi="/scripts/up.cgi"> without a restart!

The script was tested and runs equally well unter MacOS X, or LinuxPPC.

#!/usr/bin/perl
#
# The line above is needed to tell the script
# where to look for the perl program
#
# After creating a script, you need to tell the computer it's
# an executable program. Do this by typing:
# chmod 755 /path_to_script/name_of_your_script


# 'uptime' is the name of a small command-line application that prints
# the time the server has been running, and the load on the cpu
# What I do below is take the output of 'uptime' and
# store it in a string variable

$result_of_uptime_command = `uptime`;

# This is the result of a typical <uptime> command.
# We are interested in the number and time unit between the words
# right after "up". (1 day in this example below)
#
# 11:38am up 1 day, 23:30, 1 user, load average: 1.39, 1.57, 1.79
# ^^^^^^
# To extract the number from the string use "split" to break it into parts
# $part1 equals everything before "up". $part2 is everything after "up"

($part1, $part2) = split(/up /,$result_of_uptime_command);

# Given the example <uptime> output from above,
# this is what we expect the parts to be:
#
# $part1 = " 11:38am up "
# $part2 = "1 day, 23:30, 1 user, load average: 1.39, 1.57, 1.79"


# Now take $part2 and split it into a number and the time units

($time_integer, $time_unit) = split(/ /,$part2);


# Given the example <uptime> output from above,
# this is what we expect the parts to be:
#
# $time_integer = "1"
# $time_unit = "day, 23:30, 1 user, load average: 1.39, 1.57, 1.79"
#
# We don't care that $time_unit has all that extra crap after the word day.
# We can easily filter it out. We only care about the first few letters
# which will tell us mins or days. Unfortunately, it won't tell us
# the number of hours. We will have to make a special case for
# 1 to 23 hours. Bummer.

# Are you calling this script from a browser? Then uncomment the next line.
#print "Content-type: text/html\n\n";


# The next chuck is an "if (then) else" loop.
# The first line asks: If the time is not empty (!= ""), then do the loop.

if ($time_integer != "")
{

# If time is one, that is 1 min or 1 day, we need don't want "s"
# The symbol =~ checks for a substring match.


if ($time_integer == "1"){

if ($time_unit =~ /min/){$min_hr_day = "minute";
}
if ($time_unit =~ /day/){$min_hr_day = "day";
}
}

# Time must not be one, so we need the plural units

else {
if ($time_unit =~ /min/){$min_hr_day = "minutes";
}
if ($time_unit =~ /day/){$min_hr_day = "days";
}
}

# Cool. We now have an integer and a time unit.
# Let's print them! We're done.

print "$time_integer $min_hr_day\n";

}

# If we get to this "else", that means we are in that awkward first 24 hour
# period and must carve the time into hours and minutes.
# For example, <uptime> lists those first hours as "2:44," so we can use split
# is based on the colon. The "chop" command gets rid of the trailing comma.

else {

($time_hours, $time_minutes) = split(/:/,$time_unit);

# Cool. We have the number of hours and minutes.
# Let's print them. We're done.

print "\n$time_hours hrs, ";
chop $time_minutes;
print "$time_minutes min\n";
}


#the end :)

One remaining flaw in the script is that there is no check for singular or plural time units for the first 1 to 23 hours. I could have monified the same loop that does this test for minutes and days, but I was lazy. :)

Download the text of the script and give it a run. Instructions to install:

Download up2.tar.gz

Open a terminal and change into the directory where the download is located.

Type:

tar cvfz up2.tar.gz
chmod 755 up.cgi
./up.cgi

If you have been booted less than an hour, you will see the number X minutes in the terminal output string. If you have been booted longer than an hour, but less than one day, the time will be formatted as X hr and X min. After one day, the format is X day(s).

Cell Phone Themes Icons Mighty Mouse Cursors Software Reviews Widgets & Widgets

Maintained by the Staff of ResExcellence. This entire site ©1997-2006 ResExcellence
Privacy Statement? Sure we gotta Privacy Statement. [an error occurred while processing this directive]