Skip to content

📦 NEW: get cpu process #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions get_cpu_process/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Get CPU Process

Functional utility and CLI tool that returns some basic process-related info to the user, using psutil.


```sh
$ ./cpu --help

Simple CPU related script written by Zac the Wise utilising psutil

Basic usage: cpu "<process-name>"

Example: cpu "amethyst"
Returns 'running' or 'not running'


Advanced usage: cpu <option> <arguement>

--get-pid "<process-name>" returns the process id

--run-time "<process-name>"> returns the process running-time
```
44 changes: 44 additions & 0 deletions get_cpu_process/cpu
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


# SOURCE: https://thispointer.com/python-check-if-a-process-is-running-by-name-and-find-its-process-id-pid/

# pip install psutil

import sys
import argparse
from get_cpu_process import get_pid, get_process_run_time, is_running

parser = argparse.ArgumentParser(
description="Simple CPU related script written by Zac the Wise utilising psutil"
)

# set arguements
parser.add_argument(
"--get-pid", action='store_true',
help="returns PID of process name"
)
parser.add_argument(
"--run-time", action='store_true',
help="returns run time of process name"
)
parser.add_argument(
"process_name",
help="when used alone, returns True if process is running"
)

# parse args
args = parser.parse_args()

if args.get_pid:
print(get_pid(provided_process_name=args.process_name))
sys.exit()

if args.run_time:
print(get_process_run_time(provided_process_name=args.process_name))
sys.exit()

if args.process_name:
print(is_running(provided_process_name=args.process_name))
sys.exit()
44 changes: 44 additions & 0 deletions get_cpu_process/get_cpu_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import psutil
import datetime as DT

# SOURCE: https://thispointer.com/python-check-if-a-process-is-running-by-name-and-find-its-process-id-pid/

# pip install psutil


def is_running(provided_process_name: str) -> bool:
"""
Takes the name of a process and
returns True if it is running,
False if it isn't
"""
for process in psutil.process_iter():
if provided_process_name.lower() == process.name().lower():
return True
return False


def get_pid(provided_process_name: str) -> int:
"""
Takes the name of a process and
returns the process id if it
is running
"""
for process in psutil.process_iter():
if provided_process_name.lower() == process.name().lower():
return process.pid
return "Process not found"


def get_process_run_time(provided_process_name: str) -> str:
"""
Takes the name of a process and
returns the process runtime
"""
for process in psutil.process_iter():
if provided_process_name.lower() == process.name().lower():
epoch_created_time = process.create_time()
dt_created_time = DT.datetime.fromtimestamp(epoch_created_time)
time_elapsed = DT.datetime.now() - dt_created_time
return str(time_elapsed).rsplit('.')[0]
return "Process not found"