3

How to get via terminal log of system boot time for last month. I tried to use last boot command, bit it returned information from the beginning of month.

6

I know Ubuntu has systemd now, but I haven't tested this out on Ubuntu. If you have systemd then the following command should work.

systemd-analyze

If you are trying to increase boot performance then you might want to look at the output of

systemd-analyze blame

For more info see the Arch Wiki

3

In Ubuntu the file /var/log/wtmp (last gets data from this file by default) is rotated by logrotate following this configuration :

/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

As you can see it will be rotated monthly, with only one rotational file will exist meaning /var/log/wtmp will contain current month's logs while /var/log/wtmp.1 will contain previous month's logs. No logs prior to that will be saved. you can change this configuration to suite your need if you want.

Now while you run last command you will see current month's logs as the file /var/log/wtmp is read by last by default. As we know that the previous month's logs are saved in /var/log/wtmp.1, we can tell last to read from that file using -f option :

last -f /var/log/wtmp.1

Same goes for the /var/log/btmp file while using lastb.

0

last boot

who -b

lasts boot (no pun intend)

last boot
  • this will list boots from 1st to today
0

Another alternative to retrieve the system boot date consist using vmstat(8) in statistics mode with --stats argument.

The command expose the following line:

...
TIMESTAMP boot time
...

So the line can be parsed with gawk(1) and the timestamp can be converted using the date(1) command to a human readable format with the command below:

  $ TIMESTAMP=$(vmstat --stats | awk '/boot time/{print $1}')
  $ date -d @$TIMESTAMP
  or
  $ date -d @$(vmstat --stats | awk '/boot time/{print $1}')

And another alternative, by using dmesg(1) with the -T argument to render the timestamp in human readable format and search for the Command Line pattern.

The Command Line pattern line indicate the Kernel boot command line and the timestamp in the kernel ring buffer which gives the system boot time:

$ dmesg -T|grep -i 'Command line'|head -1

These approachs will work on every Linux systems, not only Ubuntu distribution. Finally, the best portable way is using who -b has mentioned previously because it's portable across systems such as Linux, Unix, OSX and FreeBSD.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.