• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

need a script to output in one column the Date and Time of last System reboot.

The output should look as follows(only one column):
-------------
Last Reboot
Dec 6 2012 08:00
--------------
0
apunkabollywood
Asked:
apunkabollywood
  • 6
  • 6
  • 4
  • +2
1 Solution
 
woolmilkporcCommented:
Did you try this?

who -b

This will print out something similar to what you posted:
who -b | awk '{printf "%-14s\nLast Reboot\n%s %s %s %s\n%-14s\n", "-------------", $3, $4, $5, $6, "-------------"}'

Do you really need the year? If so, please let me know!
0
 
apunkabollywoodAuthor Commented:
Yes I do require a exact same output in a single column

Last Reboot
Dec 6 2012 08:00
0
 
simon3270Commented:
who -b | awk '{split($3,y,"-");split($4,t,":");
b=y[1] " " y[2] " " y[3] " " t[1] " " t[2] " 0";
a=mktime(b);
printf "Last Reboot\n%s\n", strftime("%b %-e %Y %H:%M", a)}'

Open in new window


Some awk implementations don't understand the "-" in "$-e".  Normally, "%e" prints the day of the month with a leading space if the number is a single digit - the "-" suppresses that space.  If you leave it out, the in your example there would be two spaces between the "Dec" and the "6".  If your awk supports the "-", there will only be one space, as you want.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Chris SandriniSenior System EngineerCommented:
You can also do it in one go

who -b | awk '{"date -d \""$3" "$4"\" \"+%b %-e %Y %H:%M\"" | getline d; print "Last Reboot "d}'

Last Reboot Jan 24 2013 13:34

Open in new window

0
 
apunkabollywoodAuthor Commented:
Many thanks unlx86 but it works only on rhel 6 ..not working on rhel 5 and 4 please help :(
0
 
simon3270Commented:
Ah, the awk is missing some of the GNU extensions.  Does the following work?
who -b | awk '{split($3,y,"-");
m=substr("  JanFebMarAprMayJunJulAugSepOctNovDec", 3*y[2], 3);
d=sprintf("%d", y[3]);
printf "Last Reboot\n%s %s %s %s\n", m, d, y[1], $4}'

Open in new window

This could be done on one line and with fewer assignments, but that would be harder to read.
0
 
Chris SandriniSenior System EngineerCommented:
Well then this. It works also on RHEL5

date -d "`who -b | awk '{print $(NF-1),$NF}'`" "+Last Reboot %b %-e %Y %H:%M"

Open in new window

0
 
Chris SandriniSenior System EngineerCommented:
Actually the previous example only works on certain locale settings. This version should work on all locales

date -d "`who -b | sed "s/^.*system boot  \(.*\)/\1/g"`" "+Last Reboot %b %-e %Y %H:%M"

Open in new window

0
 
FishMongerCommented:
What's your definition of a column?  Most would say your example output has 4 columns.

You're probably looking for a piped shell command such has already been provided, however, since you tagged the Perl topic area, I'll provide one of the possible perl solutions.

perl -MPOSIX -MUnix::Uptime -e 'print strftime("Last Reboot\n%b %-e %Y %H:%M\n", localtime(time - Unix::Uptime->uptime))'

Open in new window

On my CentOS system outputs:
Last Reboot
Jan 14 2013 15:55
0
 
simon3270Commented:
I think we've all missed what wmp knew in the first response - the "who -b" on RHEL 4 doesn't return the year:

[sar@localhost ~]$ who -b
         system boot  Jan 30 07:39
[sar@localhost ~]$

(I've only just downloaded and set up a CentOS 4.8 VM to try it out).
0
 
Chris SandriniSenior System EngineerCommented:
simon3270:

Not even when specifying LANG=en_US?

LANG=en_US who -b

Open in new window

0
 
simon3270Commented:
No, unix86 - it's a completely vanilla install, so that was the LANG setting anyway.

The data is almost certainly available in wtmp, but it's not printed out.  You can get the date that wtmp started, because the "last" command prints out a line like:
    wtmp begins Wed Jan 30 07:39:55 2013
but that is not reset after a reboot, so you'd need to scan through the "last" output looking for "system boot" lines and try to work out the year of the most recent one.  With the stability of Rhel 4 systems, this may be several years ago!
0
 
FishMongerCommented:
Have any of you tested the perl solution I posted?  It doesn't fall prey to the issues you're seeing with parsing the output of the who -b command.
0
 
Chris SandriniSenior System EngineerCommented:
This is the uptime version taking /proc/uptime into account

date -d "`awk '{print $1}' /proc/uptime` second ago" "+Last Reboot %b %-e %Y %H:%M"

Open in new window

0
 
simon3270Commented:
@unix86 - the /proc/uptime version looks good - all you need is a "%n" in the date format to put a newline after the "Last Reboot" text.

@FishMonger I don't have the Uptime Perl module installed, so can't test that.
0
 
Chris SandriniSenior System EngineerCommented:
Well :D

date -d "`awk '{print $1}' /proc/uptime` second ago" "+Last Reboot%n%b %-e %Y %H:%M"

Open in new window


Though. last reboot or uptime differ slightly. It depends how accurate the output has to be. /proc/uptime is saving the seconds since the kernel has been booted.

@ FishMonger: It works but needs some perl modules installed.
0
 
FishMongerCommented:
The module is easily installed via this command:

[root@099-91-RKB01 ~]# cpan Unix::Uptime
0
 
simon3270Commented:
Tried the cpan command.  It wouldn't build because it couldn't find Module/Build.pm, and a "cpan Module::Build" ran for a long time and eventually failed accessing Perl/OSType.pm.  Perl is 5.8.5 on CentOS 4.8 - is that relevant?
0
 
FishMongerCommented:
Ya, that's a pretty old (outdated) perl (circa 2004).  Current release is 5.17.x  and there have been many many changes/improvements between those versions.

Upgrading perl would be best, but I suspect that you don't want to do that at this time.
0
 
apunkabollywoodAuthor Commented:
Thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 6
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now