Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> Uptime | Cut, How to show condensed uptime
Termina
post Mar 16 2004, 10:24 AM
Post #1


RMS is my Hero
******

Group: Support Specialist
Posts: 862
Joined: 18-February 04
From: Wisconsin
Member No.: 2,404



uptime | cut -c -38

Shows (usually) most of uptime, with "load average 0.0 0.0 0.0" cut out. But since, depending on how many users are logged on, and what time/date it is, sometimes using this command can cut off past of the word "users". sad.gif

I'm not very good at stream-editing, or whatever it's called, so I was wondering if anyone knew of a good tutorial, or could help me out with this problem? biggrin.gif Thanks!


--------------------
*Points finger at the author above him* They're a witch! Burn them!
---
Vist my website!
Join me in IRC! Server: st0rage.org Channel: #UnhandledExceptions
Go to the top of the page
 
+Quote Post
Corey
post Mar 16 2004, 01:52 PM
Post #2


Its GNU/Linuxhelp.net
*******

Group: Admin
Posts: 1,254
Joined: 21-September 02
From: St John's, Newfoundland, Canada
Member No.: 3



There are several different ways to do this, some more complicated than others.

Using Awk:
[quote]
#!/bin/bash

uptime
| awk '{
  # chops off "up" and everything before it:
  sub(/.*up[ ]+/,"",$0)
  # chops off ", # users" and everything after it:     
  sub(/,[ ]+[0-9]+ user.*/,"",$0)
  # cleans up extra spaces, i think:   
  sub(/,/,"",$0)
  # obvious enough, prints the results                     
  print("Uptime:",$0)
}'
[/quote]

Sed:
[quote]
uptime | sed -e 's/.* (.* days,)? (.*:..,) .*/1 2/' -e's/,//g' -e 's/ days/d/' -e 's/ up //'
[/quote]

Bash:
[quote]
#!/bin/bash
#
#  upt - show just the system uptime, days, hours, and minutes

let upSeconds="$(cat /proc/uptime) && echo ${temp%%.*})"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
if [ "${days}" -ne "0" ]
then
  echo -n "${days}d"
fi
echo -n "${hours}h${mins}m"
[/quote]

Perl
[quote]
#!/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/htmlnn";


# 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_dayn";

}

# 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 minn";
}
[/quote]

Note: i haven't tested any of these, so check them out smile.gif


--------------------
Corey Quilliam
(former) Linuxhelp.ca Administrator
cquilliam-AT-gmail-dot-com

Want to help out Linuxhelp.net? Check out our Linuxhelp Wiki and see if there are some articles you would like to submit!!

--
Ubuntu 8.04 64-bit - Work Laptop (HP-Compaq NC6400 Core2)
Kubuntu 8.04 64-bit - Desktop (HP m8120n QuadCore)
Ubuntu 6.04 - Server (I'm not upgrading this baby until support runs out in 2012) (Some old POS dell)
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 22nd October 2017 - 04:11 PM