How to accurately determine when the system was booted

It is very easy to tell how long the system has been running using uptime command, but the information when exactly it was booted is not so apparent, so I will show you two different ways to get it.

The first way - the simplest one

Use last command to display the system shutdown entries and run level changes, limit output to the boot entries, and display only the first one.

$ last -x | grep boot | head -1
reboot   system boot  3.2.0-4-amd64    Sat Oct 19 12:44 - 23:24 (8+11:40) 
System was booted at Sat Oct 19 12:44, current time is 23:24, and the uptime is 8 days, 11 hours, and 40 minutes.

The following examples demonstrate two simple ways to print the time when the system was booted, each one using different sets of commands.

$ last -x | awk '$3 ~ /boot/  {print $5 " " $6 " " $7 " " $8; exit}'
Sat Oct 19 12:44
$ last -x | grep -m 1 boot | tr -s ' ' | cut -d ' ' -f 5-8
Sat Oct 19 12:44

The second way - the more interesting one

Alternative solution is to read the /proc/uptime file, where the first number is the total number of seconds the system has been up and running.

$ cat /proc/uptime 
735751.39 778420.68
System was up and running for the 735751 seconds which equals to 8 days, 12 hours, 22 minutes.

You can accurately determine when the system was booted using date command.

$ date --date "now - `cut -d ' ' -f 1 /proc/uptime` seconds"
Sat Oct 19 12:43:25 CEST 2013

The above-mentioned command construction shortly explains why I think that this solution is more interesting.

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Red Hat 8 How to Set Up Automatic Updates for CentOS 8
Viewed 3772 times since Fri, Sep 25, 2020
Kernel sysctl configuration file for Linux
Viewed 5300 times since Fri, Aug 3, 2018
Top 20 OpenSSH Server Best Security Practices - good article
Viewed 10722 times since Mon, Oct 1, 2018
Method 2 – Use shell scripts How to install yum cron on a CentOS/RHEL 6.x/7.x
Viewed 4172 times since Tue, Dec 4, 2018
RHEL: Allowing users to ’su’ to "root" / Allowing ’root’ to login directly to the system using ’ssh’
Viewed 2906 times since Sat, Jun 2, 2018
10 Linux rsync Examples to Exclude Files/Directories
Viewed 10948 times since Wed, Oct 31, 2018
Tcpdump Examples Linux
Viewed 5903 times since Fri, Nov 16, 2018
10 nmap Commands Every Sysadmin Should Know
Viewed 9984 times since Wed, May 22, 2019
10 Linux nslookup Command Examples for DNS Lookup
Viewed 10263 times since Sun, Sep 30, 2018
Split and Reassemble files
Viewed 3528 times since Mon, May 28, 2018