Solved

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

Posted on 2013-01-30
20
478 Views
Last Modified: 2013-02-18
The output should look as follows(only one column):
-------------
Last Reboot
Dec 6 2012 08:00
--------------
0
Comment
Question by:apunkabollywood
  • 6
  • 6
  • 4
  • +2
20 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38834284
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
 

Author Comment

by:apunkabollywood
ID: 38834291
Yes I do require a exact same output in a single column

Last Reboot
Dec 6 2012 08:00
0
 
LVL 19

Expert Comment

by:simon3270
ID: 38834389
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
 
LVL 11

Expert Comment

by:un1x86
ID: 38834486
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
 

Author Comment

by:apunkabollywood
ID: 38834795
Many thanks unlx86 but it works only on rhel 6 ..not working on rhel 5 and 4 please help :(
0
 
LVL 19

Expert Comment

by:simon3270
ID: 38834933
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
 
LVL 11

Expert Comment

by:un1x86
ID: 38835026
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
 
LVL 11

Expert Comment

by:un1x86
ID: 38835038
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 38835541
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
 
LVL 19

Expert Comment

by:simon3270
ID: 38835913
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Expert Comment

by:un1x86
ID: 38835916
simon3270:

Not even when specifying LANG=en_US?

LANG=en_US who -b

Open in new window

0
 
LVL 19

Expert Comment

by:simon3270
ID: 38835973
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 38836020
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
 
LVL 11

Expert Comment

by:un1x86
ID: 38836063
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
 
LVL 19

Expert Comment

by:simon3270
ID: 38836158
@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
 
LVL 11

Accepted Solution

by:
un1x86 earned 500 total points
ID: 38836173
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 38836260
The module is easily installed via this command:

[root@099-91-RKB01 ~]# cpan Unix::Uptime
0
 
LVL 19

Expert Comment

by:simon3270
ID: 38837391
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 38837518
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
 

Author Closing Comment

by:apunkabollywood
ID: 38904370
Thank you
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now