+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 28

Find time of last boot

  1. Find time of last boot

    Dear All,

    I need to find the time of the last boot in a shell script.

    "uptime" reports the time since the last boot, which it gets (in
    seconds-since-the-epoch) from /proc/uptime. So I could subtract this
    value from the current time. But there's a danger of being slightly
    wrong if time has advanced between reading the uptime and reading the
    current time, and it's important that I get consistent results. So I
    used "last -1 reboot", which did what I wanted -- until the machine had
    been up for more than a month, and the old wtmp entries were removed.

    (I was wondering whether the kernel internally recorded the time of last
    boot in order to calculate the uptime by subtracting this from the
    current time, but having had a quick look at the source it seems that it
    separately records the current time and the uptime and increments both
    on timer interrupts. This means that uptime is correct if you change
    the date, which is probably what you want.)

    So, I thought I would ask and see if anyone has any alternative ideas
    about how to do this. Any suggestions?

    Cheers,

    Phil.

  2. Re: Find time of last boot

    Phil Endecott writes:
    > I need to find the time of the last boot in a shell script.


    Install an init script that saves the boot time at boot.
    --
    John Hasler
    john@dhh.gt.org
    Dancing Horse Hill
    Elmwood, WI USA

  3. Re: Find time of last boot

    In article <874po949j0.fsf@toncho.dhh.gt.org>,
    John Hasler wrote:
    >Phil Endecott writes:
    >> I need to find the time of the last boot in a shell script.

    >
    >Install an init script that saves the boot time at boot.


    This is good advice, because there are a lot of pitfalls (*) associated
    with trying to get the boot time directly via the various fields in
    /proc (or, obviously, by using utilities which get their info from
    /proc). I've been using what John suggests for quite some time now.

    (*) For example, how many people here know that Linux uptime wraps
    around after 1.361 years (on 32 bit systems) ?


  4. Re: Find time of last boot

    Phil Endecott wrote:
    > Dear All,
    >
    > I need to find the time of the last boot in a shell script.
    >
    > "uptime" reports the time since the last boot, which it gets (in
    > seconds-since-the-epoch) from /proc/uptime. So I could subtract this
    > value from the current time. But there's a danger of being slightly
    > wrong if time has advanced between reading the uptime and reading the
    > current time, and it's important that I get consistent results. So I
    > used "last -1 reboot", which did what I wanted -- until the machine had
    > been up for more than a month, and the old wtmp entries were removed.
    >
    > (I was wondering whether the kernel internally recorded the time of last
    > boot in order to calculate the uptime by subtracting this from the
    > current time, but having had a quick look at the source it seems that it
    > separately records the current time and the uptime and increments both
    > on timer interrupts. This means that uptime is correct if you change
    > the date, which is probably what you want.)
    >
    > So, I thought I would ask and see if anyone has any alternative ideas
    > about how to do this. Any suggestions?
    >
    > Cheers,
    >
    > Phil.


    If it is withing about the last month, look at /var/log/messages and
    /var/log/messages.[1-4]. Grep for a line that says something like:

    Feb 25 03:04:04 YourSystemName syslogd 1.4.1: restart.

    The date is to the left. If there is more than one ...syslogd...restart.,
    then the most recent one is the one you want. If there is not any, then it
    was over 5 weeks ago.

    --
    .~. Jean-David Beyer Registered Linux User 85642.
    /V\ PGP-Key: 9A2FC99A Registered Machine 241939.
    /( )\ Shrewsbury, New Jersey http://counter.li.org
    ^^-^^ 15:10:02 up 37 days, 2:35, 4 users, load average: 4.28, 4.25, 4.19

  5. Re: Find time of last boot

    On Mar 25, 11:18 am, Phil Endecott
    wrote:
    > Dear All,
    >
    > I need to find the time of the last boot in a shell script.


    So, parse the output from last(1)

    LAST,LASTB(1) Linux System Administrator's Manual
    LAST,LASTB(1)

    NAME
    last, lastb - show listing of last logged in users

    ...
    DESCRIPTION
    Last searches back through the file /var/log/wtmp (or the
    file desig-
    nated by the -f flag) and displays a list of all users
    logged in (and
    out) since that file was created. Names of users and
    tty's can be
    given, in which case last will show only those entries
    matching the
    arguments. Names of ttys can be abbreviated, thus last 0
    is the same
    as last tty0.

    ...

    The pseudo user reboot logs in each time the system is
    rebooted. Thus
    last reboot will show a log of all reboots since the log file
    was cre-
    ated.
    ------------

    ~ $ last reboot
    reboot system boot 2.4.33.3 Sun Mar 25 13:03
    (02:16)

    wtmp begins Sun Mar 25 10:18:28 2007
    ~ $

    or read it directly from wtmp

    [snip]
    > So, I thought I would ask and see if anyone has any alternative ideas
    > about how to do this. Any suggestions?




  6. Re: Find time of last boot

    Lew Pitcher wrote:
    > On Mar 25, 11:18 am, Phil Endecott
    > wrote:
    >> Dear All,
    >>
    >> I need to find the time of the last boot in a shell script.

    >
    > So, parse the output from last(1)


    Lew, you should have read the rest of my post:

    I used "last -1 reboot", which did what I wanted -- until the
    machine had been up for more than a month, and the old wtmp entries
    were removed.

  7. Re: Find time of last boot

    John Hasler wrote:
    > Phil Endecott writes:
    >> I need to find the time of the last boot in a shell script.

    >
    > Install an init script that saves the boot time at boot.


    Good idea. Slight snag is that it makes the system as a whole more
    difficult to install.

    It gives me another idea. Is there a file that is reliably (re-)created
    at each boot that I can look at the timestamp of?


    Phil.

  8. Re: Find time of last boot

    Phil Endecott wrote:
    > Lew Pitcher wrote:
    >> On Mar 25, 11:18 am, Phil Endecott
    >> wrote:
    >>> Dear All,
    >>>
    >>> I need to find the time of the last boot in a shell script.

    >>
    >> So, parse the output from last(1)

    >
    > Lew, you should have read the rest of my post:
    >
    > I used "last -1 reboot", which did what I wanted -- until the
    > machine had been up for more than a month, and the old wtmp entries
    > were removed.


    Why don't you put something like "last > /var/log/last.txt" in rc.local?
    Of course it gets overwritten at each reboot, but if I understand
    correctly, that's the only thing you're interested in, isn't it?

    Michel.

  9. Re: Find time of last boot

    In Phil Endecott:

    [Snip...]

    > at each boot that I can look at the timestamp of?


    IIRC, SuSE has used /var/log/boot.msg in this way for years--HTH; YMMV...

    --
    Regards, Weird (Harold Stevens) * IMPORTANT EMAIL INFO FOLLOWS *
    Pardon any bogus email addresses (wookie) in place for spambots.
    Really, it's (wyrd) at airmail, dotted with net. DO NOT SPAM IT.
    Kids jumping ship? Looking to hire an old-school type? Email me.

  10. Re: Find time of last boot

    Jean-David Beyer writes:

    >Phil Endecott wrote:
    >> Dear All,
    >>
    >> I need to find the time of the last boot in a shell script.
    >>
    >> "uptime" reports the time since the last boot, which it gets (in
    >> seconds-since-the-epoch) from /proc/uptime. So I could subtract this
    >> value from the current time. But there's a danger of being slightly
    >> wrong if time has advanced between reading the uptime and reading the


    "time has advanced"? I would hope so, or your uptime will be very very
    small.

    Anyway I have no idea what the concern is. uptime and current time are in
    UTC. It has nothing to do with daylight saving or anything like that, if
    that is what your worry is. last on the other hand reports the time in
    local time which means that it records the time with dst.


    >> current time, and it's important that I get consistent results. So I


    What does "consistant" mean to you?

    >> used "last -1 reboot", which did what I wanted -- until the machine had
    >> been up for more than a month, and the old wtmp entries were removed.
    >>
    >> (I was wondering whether the kernel internally recorded the time of last
    >> boot in order to calculate the uptime by subtracting this from the
    >> current time, but having had a quick look at the source it seems that it
    >> separately records the current time and the uptime and increments both
    >> on timer interrupts. This means that uptime is correct if you change
    >> the date, which is probably what you want.)


    I think that you need to tell us what it is that you want.

    >>
    >> So, I thought I would ask and see if anyone has any alternative ideas
    >> about how to do this. Any suggestions?
    >>
    >> Cheers,
    >>
    >> Phil.


    >If it is withing about the last month, look at /var/log/messages and
    >/var/log/messages.[1-4]. Grep for a line that says something like:


    >Feb 25 03:04:04 YourSystemName syslogd 1.4.1: restart.


    >The date is to the left. If there is more than one ...syslogd...restart.,
    >then the most recent one is the one you want. If there is not any, then it
    >was over 5 weeks ago.


    >--
    > .~. Jean-David Beyer Registered Linux User 85642.
    > /V\ PGP-Key: 9A2FC99A Registered Machine 241939.
    > /( )\ Shrewsbury, New Jersey http://counter.li.org
    > ^^-^^ 15:10:02 up 37 days, 2:35, 4 users, load average: 4.28, 4.25, 4.19


+ Reply to Thread
Page 1 of 3 1 2 3 LastLast