Overview
This article provides a few examples of the ps command, which can help keep track of Memory and CPU usage on your VPS.
For example, if you notice that your memory usage went from 60MB to 250MB in one day, you should run the following commands to examine the cause.
Prerequisites
This article assumes you have created a shell user and are able to log into your server via SSH.
How to use the 'ps' command
The following commands help to determine the cause of high memory and CPU usage.
Displaying processes
-
These commands display the top ten (10) processes using memory.
Using pcpu
The pcpu option displays the percentage of CPU time used by each process.
[server]$ ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10 [server]$ ps -eo pcpu,pid,user,args | sort -r -k1 | less
Using pmem
The pmem option displays the percentage of physical memory used by each process.
[server]$ ps -eo pmem,pid,user,args | sort -k 1 -r | head -10 [server]$ ps -eo pmem,pid,user,args | sort -r -k1 | less
This command displays USERNAME, CPU%, MEMORY%, and the number of processes running.
[server]$ ps aux | awk '{cpu[$1]+=$3; mem[$1]+=$4; procs[$1]+=1} END { for (user in cpu){ print user,"cpu:",cpu[user],"mem:",mem[user],"proc:",procs[user] } }'
Displaying memory usage
-
These commands display memory usage.
[server]$ ps aux --sort pmem
View memory usage over time (requires you to repeat the command or usage of a cron job):
[server]$ ps ev --pid=[EnterPID]
Displays all currently running processes:
[server]$ ps -ef
and on most systems:
[server]$ ps -aux