decentralizuj
Posted on April 7, 2021
If you ever found yourself googling how to find active processes in linux, or process with the highest CPU, save this post as a reference, and save code into the script.
COMMANDS:
ps aux
#=> list processes
grep 'firefox'
#=> show firefox processes
awk *opts
#=> choose what we want to print
We will use this simple commands, so let's make a script for this:
#!/usr/bin/env bash
# Define 'help' banner
function print_help() {
echo -e "--------------------------------------------------------------"
echo -e " - procfind will print the highest CPU% processes"
echo -e " - procfind firefox - will print firefox processes"
echo -e "--------------------------------------------------------------"
}
# max number of processes to show
processes="10"
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
print_help
exit
else
# If first argument is passed as string, search processes for that arg
echo "CPU |PID |MEM |START |COMMAND"
ps aux | grep "$1"| awk '{print $3,$2,$4,$9,$11}' | sort -rn | head -n "$processes"
fi
exit
Save script as you want (procfind) and give it executable permissions with sudo chmod +x procfind
. No .sh
at the end because we defined our environment #!/usr/bin/env bash
.
Now if we run ./procfind -h
, we will get:
------------------------------------------------------------
- ./procfind will print the highest CPU% processes
- ./procfind firefox - will print firefox processes
------------------------------------------------------------
If we run ./procfind firefox
:
CPU |PID |MEM |START |COMMAND
20.2 24281 10.1 16:04 /home/USERNAME/PATH/tor-browser_en-USA/Browser/firefox.real
11.8 24209 12.5 16:03 ./firefox.real
1.9 24325 9.8 16:04 /home/USERNAME/PATH/tor-browser_en-USA/Browser/firefox.real
1.4 24361 5.2 16:04 /home/USERNAME/PATH/tor-browser_en-USA/Browser/firefox.real
0.7 31245 6.0 18:25 /home/USERNAME/PATH/tor-browser_en-USA/Browser/firefox.real
0.0 376 0.0 19:21 grep
💖 💪 🙅 🚩
decentralizuj
Posted on April 7, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.