Get date/time of last reboot programmatically
Asked Answered
I

3

6

I would like to find the time of the last reboot programmatically in Linux. I am using C and C++ in my module.

Is there an API?

Ignorant answered 11/2, 2011 at 18:25 Comment(0)
W
1

There's no direct api, but you can read /proc/uptime "file" to learn how many seconds the system have been running, subtract that from the current time - and assume that's when the machine was rebooted.

/proc/uptime This file contains two numbers: the uptime of the system (seconds), and the amount of time spent in idle process (seconds).

Wisnicki answered 11/2, 2011 at 18:32 Comment(0)
J
5

You could either invoke uptime or take a look at /proc/uptime. Given the current time, and knowing how long the system's been up allows you to calculate the time of the last reboot.

Alternatively, the btime entry in /proc/stat gives the boot time directly (in seconds since the epoch) -- search man proc for btime.

Jestinejesting answered 11/2, 2011 at 18:29 Comment(0)
W
1

There's no direct api, but you can read /proc/uptime "file" to learn how many seconds the system have been running, subtract that from the current time - and assume that's when the machine was rebooted.

/proc/uptime This file contains two numbers: the uptime of the system (seconds), and the amount of time spent in idle process (seconds).

Wisnicki answered 11/2, 2011 at 18:32 Comment(0)
A
0

Also, you can find the last reboot time by reading the /var/log/wtmp file. This is a binary file but you can parse with it (or your C version of it)

http://pastebin.com/nSVw82cZ

[WARNING! terrible coding style!]

Adversity answered 11/2, 2011 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.