Bash script
#!/bin/bash
# Function to sort processes by memory usage
function sort_by_memory() {
# Get the process information and sort by memory usage
ps -eo pid,comm,pmem –sort=-pmem | head
}
# Call the function to get the list of processes sorted by memory usage
sort_by_memory
This script uses the ps command to get the process information, including the process ID (pid), the process name (comm), and the percentage of physical memory used by the process (pmem). The information is sorted in descending order by the memory usage (pmem) and the top 10 processes with the highest memory usage are displayed.
You can adjust the number of processes displayed by changing the number after the head command in the script.