It is important to keep track of long running processes on your system, whether you are a system administrator or not. Long running processes take up a lot of CPU processing and memory, and can slow down your system. Sometimes they may even stop working and slow down your system. In this article, we will check long running processes in Linux. Once you have determined the long running processes on your system, you can kill them if they have become non-responsive. You can use these steps on almost every Linux system.
How to Check Long Running Processes in Linux
We will mainly use ps command to identify long running processes in Linux. It stands for process status and displays the information about active/running processes. It provides useful details such as username, PID, cpu usage, memory usage, process start date & time, etc.
1. Shell Script to Check How Long High CPU Consumption Processes Run in Linux
Here is the command to find out list of users running processes and save the output to /tmp/long-running-processes.txt file.
ps -eo pid,user,ppid,%mem,%cpu,cmd --sort=-%cpu | head | tail -n +2 | awk '{print $1}' > /tmp/long-running-processes.txt
You can use this information to get process run time by user and commands.
Create an empty shell script.
$ sudo vi long-process-cpu-usage.sh
Add the following lines to it.
#!/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 "--------------------------------------------------"
Save and close the file. Make it an executable with the following command.
$ sudo chmod +x long-process-cpu-usage.sh
When you run the above script, you will see the following output.
# sh /opt/scripts/long-running-cpu-proc.sh ---------------------------------------------------- UName PID CMD Process_Running_Time ---------------------------------------------------- Ubuntu 3214 Web 01:18:48 Ubuntu 3748 Web 01:08:20 Ubuntu 1639 firefox 01:44:51 Ubuntu 3793 nautilus 24:14 Ubuntu 3301 Web 57:40 ----------------------------------------------------
2. Shell Script to Check Top Memory Consuming Processes
Here is a shell script you can use to get top memory consuming processes on your system.
$ sudo vi long-running-memory-process.sh
Add the following lines to it.
#!/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 "--------------------------------------------------"
Save and close the file. Make the file executable.
$ chmod +x long-running-memory-process.sh
When you run the script you will see the following output.
# sh /opt/scripts/long-running-memory-process.sh ---------------------------------------------------- UName PID CMD Process_Running_Time ---------------------------------------------------- ubuntu 3639 firefox 08:44:56 ubuntu 3997 Web 07:39:54 ubuntu 3269 Web 09:18:37 ubuntu 2712 Web 10:44:55 ----------------------------------------------------
In this article, we have looked at two shell scripts to get memory consumption of long running processes.
Also read:
How to Backup & Restore Hard Disk in Linux
How to Lock & Unlock Users in Linux
How to Change FTP Port in Linux
How to Check CVE Vulnerability in Linux
How to Capture Top Command Output in Linux