Skip to content
Related Articles

Related Articles

Improve Article

Python Script to check PC last reboot time

  • Difficulty Level : Medium
  • Last Updated : 28 Jul, 2021

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :