Find last system shut down time


 
Thread Tools Search this Thread
Operating Systems Linux Find last system shut down time
# 1  
Old 01-11-2011
Find last system shut down time

Hi,

I need to find the last system shutdown time. I got a command last that is used for this.But the command is not give the year. Below i posted the sample output

Code:
last -x |grep shutdown
shutdown system down  2.6.31.5-server- Tue Jan 11 11:45 - 11:46  (00:00)
shutdown system down  2.6.31.5-server- Sat Dec 11 11:25 - 11:45 (31+00:20)
shutdown system down  2.6.31.5-server- Mon Jan 10 21:04 - 10:31  (13:27)
shutdown system down  2.6.31.5-server- Fri Jan  7 17:38 - 11:05 (2+17:26)
shutdown system down  2.6.31.5-server- Thu Jan  6 20:39 - 10:00  (13:21)
shutdown system down  2.6.31.5-server- Wed Jan  5 19:48 - 10:46  (14:58)
shutdown system down  2.6.31.5-server- Tue Jan  4 20:13 - 09:51  (13:38)

year field is important for me. how can i get that?

Guide me.

Moderator's Comments:
Mod Comment Please use code tags for listings and console output.

Last edited by pludi; 01-11-2011 at 07:32 AM..
# 2  
Old 01-11-2011
If you just need last shutdown; try using the command:
Code:
uptime

you can do some calculation with curent date-time to get the exact shutdown date.

Note: There might be few hours difference; as shutdown time and uptime would not be same; this difference of hours would be system downtime.
This may result in date change also, if shutdown occured around midnight or system downtime was fairly large.

Hope this helps.

Last edited by freakygs; 01-11-2011 at 07:03 AM.. Reason: spell error
# 3  
Old 01-12-2011
Thanks for your reply

uptime
command gives only how long the system is running. By uptime result, we can only decide when the system is started exactly.

How can we calculate system last down time using uptime result?

Gudie me.

---------- Post updated 01-12-11 at 01:28 AM ---------- Previous update was 01-11-11 at 07:23 AM ----------

Hi, any update on this.
# 4  
Old 01-12-2011
uptime command would give the output in following format:

Quote:
01:40AM up X days, 9:33, 3 users ...
You would need to subtract X days from current date to get the last reboot date.

Alternatively, You can use the following command to determine last reboot time, with year.

Code:
errpt -aj 2BFA76F6

2BFA76F6: happens to be an identifier for SYSTEM SHUTDOWN in AIX, and can be obtained using:

Code:
errpt | grep SHUT


Last edited by freakygs; 01-12-2011 at 03:50 AM.. Reason: more information added
# 5  
Old 01-12-2011
Thanks for your reply.

But i need for Linux system. What command is used to get?
# 6  
Old 01-16-2011
Have a look at the "fwtmp" command. This can be used to read the file which "last" uses and outputs the history in text format - including the full date.
# 7  
Old 01-16-2011
You are correct, maruthu:
Code:
trogdor ~ $ last -1x shutdown
shutdown system down  2.6.34.7-66.fc13 Sat Jan  8 20:20 - 20:29  (00:09)

wtmp begins Sun Aug 29 06:30:04 2010

Gives you the time of the last shutdown.

But:
Code:
trogdor ~ $ last -1x reboot
reboot   system boot  2.6.34.7-66.fc13 Sat Jan  8 20:29 - 10:18 (7+13:49)   

wtmp begins Sun Aug 29 06:30:04 2010

Shows the time of the last (re)boot, which corresponds to what is shown by uptime:
Code:
trogdor ~ $ uptime
 10:18:41 up 7 days, 13:49,  6 users,  load average: 0.03, 0.09, 0.08

You can use awk, sed, perl, and numerous other tools to extract and format the date and time of the last reboot to your liking. Please note that the year is not displayed.

You can parse /var/log/wtmp to get what you need. For instance, using the examples in Formatting and Printing Wtmp as a starting point:
Code:
#! /usr/bin/perl

use strict;
use warnings;
use POSIX qw{ strftime };

undef $/;

$\ = "\n";
$, = '';

my $reclen   = 384; # sizeof (struct utmp)
my $linesize =  32; # value of UT_LINESIZE in /usr/include/bits/utmp.h
my $namesize =  32; # value of UT_NAMESIZE in /usr/include/bits/utmp.h
my $hostsize = 256; # value of UT_HOSTSIZE in /usr/include/bits/utmp.h

my $wtmpfile = '/var/log/wtmp';

my $entry_template = "(a$reclen)*";
my $utmp_template  = "I x4 x$linesize x4 x$namesize x$hostsize x4 x4 I";

my @UT_TYPE  = qw{ Empty RunLvl Boot NewTime OldTime Init Login Normal Term Account };

open FH, '<', $wtmpfile or die $wtmpfile;

my $boot = undef;
foreach my $entry (unpack $entry_template, <FH>) {
    my ($type, $when) = unpack $utmp_template, $entry;
    $boot = $when if $UT_TYPE[$type] eq 'Boot';
}

close FH;
print strftime '%Y-%m-%d %H:%M:%S %Z', localtime $boot if defined $boot;

will give you the time of the reboot:
Code:
trogdor ~ $ lastboot
2011-01-08 20:29:54 EST

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Find gaps in time data and replace missing time value and column 2 value by interpolation in awk

Dear all, I am kindly seeking assistance on the following issue. I am working with data that is sampled every 0.05 hours (that is 3 minutes intervals) here is a sample data from the file 5.00000 15.5030 5.05000 15.6680 5.10000 16.0100 5.15000 16.3450 5.20000 16.7120 5.25000... (4 Replies)
Discussion started by: malandisa
4 Replies

2. Linux

Time: Hwclock and System Time

Hey everyone. Upon studying linux trying to learn it inside and out, I'm reading about the issue of time. Hardware clock time vs the more commonly referenced System Time. What causes the two to grow apart, and what causes the time itself to stray away from UTC? at present my clock is a second and... (1 Reply)
Discussion started by: Lost in Cyberia
1 Replies

3. Shell Programming and Scripting

Additional time to system time

Hi All, Is there any command to add additional time to date command. I need to add 5 hours to the present system time. I am getting the time by using date command. WORKFLOW_START_TIME=`date +%m/%d/%Y\ %H:%M:%S` Thanks (8 Replies)
Discussion started by: nag_sathi
8 Replies

4. Shell Programming and Scripting

what would a script include to find CPU's %system time high and user time high?

Hi , I am trying to :wall: my head while scripting ..I am really new to this stuff , never did it before :( . how to find cpu's system high time and user time high in a script?? thanks , help would be appreciated ! :) (9 Replies)
Discussion started by: sushwey
9 Replies

5. Solaris

System time and Cron time stamp not matching

On Solaris 10 server the system date won't match with the timestamp on files created by a cron jobs, Please help here is what i get when i check for system date infodba-ie10ux014:/tcpdv1_ie10/tcadmin/bin\n\r-> date Tue Apr 24 15:27:43 GMT 2012at same time i executed a cron job, and checked... (4 Replies)
Discussion started by: karghum
4 Replies

6. Solaris

How to find out bottleneck if system is taking long time in gzip

Dear All, OS = Solaris 5.10 Hardware Sun Fire T2000 with 1 Ghz quode core We have oracle application 11i with 10g database. When ever i am trying to take cold backup of database with 55GB size its taking long time to finish. As the application is down nobody is using the server at all... (8 Replies)
Discussion started by: yoojamu
8 Replies

7. Shell Programming and Scripting

Find and Convert UTC Time to PST Time

Hello All - I have a script that grabs data from the net and outputs the following data 46029 46.144 -124.510 2010 07 26 22 50 320 4.0 6.0 2.2 9 6.8 311 1012.1 -0.9 13.3 13.5 13.3 - - 46041 47.353 -124.731 2010 07 26 22 50 250 2.0 3.0 1.6 8 6.4 - 1011.6 - ... (0 Replies)
Discussion started by: drexnefex
0 Replies

8. Solaris

getting time independent of system time in solaries

i am using function gethrtime() in sun solaries to get the time independent of the system time.Problem with this function is if we restart the system time will change to '0'.is there any other way to resolve this problem. thanks & regards suresh (3 Replies)
Discussion started by: suresh_rtp
3 Replies

9. Shell Programming and Scripting

System time comparison to fixed defined time

I have a requirement of checking the current system time and performing certain actions in a shell script. example: if the current system time is greater than 1400 hrs, then perform step 1,2,3 if the current system time is greater than 1000 hrs, then perform step 1,2 if the current system time... (2 Replies)
Discussion started by: zainravi
2 Replies

10. UNIX for Dummies Questions & Answers

Need to shut off beep

I need to know how to turn off the beep in my unix environment. I tried with the Common Desktop Environment on solaris but it didn't work. I used to know the environment variable to set but I have forgotten it. (1 Reply)
Discussion started by: jpedicone
1 Replies
Login or Register to Ask a Question