How to check high CPU/memory consumption processes in Linux

This tutorial contains two of the scripts , they can help you determine  Linux  running time high CPU / memory consumption process. The script will display the process ID, the owner of the process, the name of the process, and the running time of the process.

How to check high CPU/memory consumption processes in Linux How to check high CPU/memory consumption processes in Linux

This tutorial contains two scripts that can help you determine the running time of high CPU/memory consuming processes on Linux.

The script will display the process ID, the owner of the process, the name of the process, and the running time of the process. This will help you determine which jobs (must be completed in advance) are running overtime. This can be achieved using the ps  command .

What is the ps  command

ps is the processes status, which displays information about the active/running processes on the system.

It provides a snapshot of the current process and detailed information, such as user name, user ID, CPU usage, memory usage, process start date and time, etc.

1) Check how long the high CPU consumption process has been running on Linux Bash script

This script will help you determine how long a high CPU consuming process has been running on Linux.

# vi /opt/scripts/long-running-cpu-proc.sh
#!/bin/bash
ps -eo pid,user,ppid,%mem,%cpu,cmd --sort=-%cpu | head | tail -n +2 | awk '{print $1}' > /tmp/long-running-processes.txt
echo "--------------------------------------------------"
echo "UName PID CMD Process_Running_Time"
echo "--------------------------------------------------"
for userid in `cat /tmp/long-running-processes.txt`
do
username=$(ps -u -p $userid | tail -1 | awk '{print $1}')
pruntime=$(ps -p $userid -o etime | tail -1)
ocmd=$(ps -p $userid | tail -1 | awk '{print $4}')
echo "$username $userid $ocmd $pruntime"
done | column -t
echo "--------------------------------------------------"

Set the executable Linux file permissions for long-running-cpu-proc.sh.

# chmod +x /opt/scripts/long-running-cpu-proc.sh

When you run this script, you will get output similar to the following:

# sh /opt/scripts/long-running-cpu-proc.sh
 
----------------------------------------------------
UName PID CMD Process_Running_Time
----------------------------------------------------
daygeek 5214 Web 01:18:48
daygeek 5748 Web 01:08:20
daygeek 8043 inkscape 22:11
daygeek 5269 Web 01:18:31
daygeek 1712 Web 10:44:50
daygeek 5335 RDD 01:17:54
daygeek 1639 firefox 10:44:51
daygeek 7793 nautilus 24:14
daygeek 6301 Web 57:40
----------------------------------------------------

2) Check how long a Bash script has been running on Linux for high memory consumption processes

This script will help you determine how long the largest memory consuming process has been running on Linux.

# sh /opt/scripts/long-running-memory-proc.sh
 
#!/bin/bash
ps -eo pid,user,ppid,%mem,%cpu,cmd --sort=-%mem | head | tail -n +2 | awk '{print $1}' > /tmp/long-running-processes-1.txt
echo "--------------------------------------------------"
echo "UName PID CMD Process_Running_Time"
echo "--------------------------------------------------"
for userid in `cat /tmp/long-running-processes-1.txt`
do
username=$(ps -u -p $userid | tail -1 | awk '{print $1}')
pruntime=$(ps -p $userid -o etime | tail -1)
ocmd=$(ps -p $userid | tail -1 | awk '{print $4}')
echo "$username $userid $ocmd $pruntime"
done | column -t
echo "--------------------------------------------------"

Set the executable Linux file permissions for long-running-memory-proc.sh.

# chmod +x /opt/scripts/long-running-memory-proc.sh

When you run this script, you will get output similar to the following:

# sh /opt/scripts/long-running-memory-proc.sh
 
----------------------------------------------------
UName PID CMD Process_Running_Time
----------------------------------------------------
daygeek 1639 firefox 10:44:56
daygeek 2997 Web 10:39:54
daygeek 5269 Web 01:18:37
daygeek 1712 Web 10:44:55
daygeek 8043 inkscape 22:17
daygeek 5214 Web 01:18:54
daygeek 1898 Web 10:44:48
daygeek 1129 Xorg 10:45:07
daygeek 6301 Web 57:45
----------------------------------------------------

This article address: https://www.linuxprobe.com/linux-ps-cpu.html

Guess you like

Origin blog.csdn.net/u014389734/article/details/107971399