VOOZH about

URL: https://thenewstack.io/monitor-your-system-health-from-pythons-command-line/

⇱ Monitor Your System Health From Python's Command Line - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2025-06-09 08:00:09
Monitor Your System Health From Python's Command Line
Programming Languages / Python / Software Development

Monitor Your System Health From Python’s Command Line

Learn how to use Python's psutil library from the command line for real-time system monitoring, checking CPU and memory usage, tracking processes and automating disk diagnostics without complex monitoring tools.
Jun 9th, 2025 8:00am by Jessica Wachtel
👁 Featued image for: Monitor Your System Health From Python’s Command Line
Featured image via Unsplash+.

Python’s psutil library is a powerful tool for getting real-time insight into your system’s performance. You can use it in custom scripts, applications or directly from the command line.

Using psutil in the command line interface (CLI) gives you speed, simplicity and flexibility. It’s especially useful for developers, system administrators and DevOps engineers who live in the terminal. It’s also a great entry point for beginners learning the CLI and exploring their system’s resource usage.

You might choose psutil in the CLI over other methods when:

  • You need diagnostics on the fly, like during a sudden CPU spike or unexpected memory usage.
  • You want to integrate system checks into shell scripts or cron jobs (for example, to detect zombie processes).
  • You prefer minimal setups instead of complex monitoring stacks.

There are more reasons to use psutil in the CLI, and many apply to other CLI-based scripts as well:

  • No GUI, less overhead. CLI scripts are lightweight, fast and work across platforms including Windows, macOS and Linux.
  • Tailor scripts to exactly what you care about — CPU usage, memory pressure, disk I/O or running processes — and output them however you like: plain text, logs or JSON.
  • Automation ready. CLI-based psutil scripts plug easily into cron, systemd timers, CI/CD pipelines or remote monitoring setups over SSH.

Using psutil in the CLI

First, make sure psutil is installed.

Before you can use the terminal to check system health metrics, you need to run Python interactively.

Type python3 in the terminal to get started:

Checking CPU Usage

Is your laptop fan running unexpectedly? It might be time to check CPU usage. High CPU activity generates heat, which triggers the fan. This could mean a background process is stuck in a loop, or worse, malware is running silently and consuming your resources.

Other signs include general sluggishness or system unresponsiveness, sudden temperature spikes or noticing zombie and defunct processes.

The code below uses psutil’s cpu_percent() function. The interval=1 means it measures CPU usage over one second to give a more accurate reading. The function returns the percentage of CPU in use, and the print statement shows it as a readable string:

This will show you the percentage of CPU you’re currently using.

Checking Memory Usage

Just like with the CPU, if your system feels slow or apps freeze or close unexpectedly, it could be time to check memory usage. High memory warnings are a clear sign to look into it.

In the code below, psutil.virtual_memory() fetches your memory stats, including total, used and available RAM. The print statements convert the values from bytes to gigabytes for easier reading:

After You Detect High Memory Usage

You can run this anytime, but it’s especially helpful after spotting high memory consumption. It shows which processes are using the most RAM.

The code goes through all running processes, filters out those not using memory, and prints a list of the ones that are, sorted by the highest memory use at the top:

Save Disk Usage Report to a Log

This is useful when you want to track slow-growing storage issues, understand when usage spikes happen, feed monitoring and alerting systems or just collect disk data automatically with minimal impact.

The code first gets the current date and time. Then psutil.disk_usage('/') pulls disk stats for the root partition (you can change /' to any path). It formats the time in ISO standard, then opens a file called disk_report.log in append mode so existing data stays intact:

You can also schedule this as a cron job to run regularly:

Conclusion

Using psutil in the CLI offers a fast, flexible way to monitor your system’s health. It’s a practical step toward staying on top of system performance with minimal setup and maximum control.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.