Open In App

Python Script to check PC last reboot time

Last Updated : 28 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

psutil is a cross-platform library for retrieving information on running processes and system utilization(CPU, memory, disks, networks, sensors) in Python. The Python script below can be run in both Windows and Linux. psutil library can be installed using the terminal by:

In Windows:

pip install psutil

In Linux:

sudo apt-get install gcc python3-dev
sudo pip3 install psutil

Code:

Python3




import psutil
import datetime
 
# returns the time in seconds since the epoch
last_reboot = psutil.boot_time()
 
# converting the date and time in readable format
print(datetime.datetime.fromtimestamp(last_reboot))


Output:

2020-08-20 16:46:48

Explanation: 

psutil.boot_time() returns the system boot time expressed in seconds since the epoch. It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds; the Unix epoch is 00:00:00 UTC on 1 January 1970. 
To convert this time to DateTime we use fromtimestamp(), which returns the local date and time.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads