Python Script to check PC last reboot time
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
last_reboot = psutil.boot_time()
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.
Share your thoughts in the comments
Please Login to comment...