Skip to content

Instantly share code, notes, and snippets.

@rkuzsma
Created June 12, 2017 00:37
Show Gist options
  • Save rkuzsma/73acae31e93fd6ac3cba1a648d71da95 to your computer and use it in GitHub Desktop.
Save rkuzsma/73acae31e93fd6ac3cba1a648d71da95 to your computer and use it in GitHub Desktop.
Bash script to fetch and sort the top CPU consuming process, displaying a concatenated form of the process startup command line arguments
# The sed expression is intended to replace this:
# %CPU %MEM COMMAND
# 0.0 0.1 some really really really long process args
# with this:
# %CPU %MEM COMMAND
# 0.0 0.1 some reall...ocess args
# In the sed regex:
# [^[:space:]]* means "a sequence non-whitespace characters"
# \s* means "a sequence of whitespace characters"
# The sed regex matches the first and last 10 characters of the process args,
# and concatenates them with an ellipsis ... for easier viewing.
echo "%CPU %MEM ARGS $(date)" && \
ps -e -o pcpu,pmem,args --sort=pcpu | \
sed -e 's/\(^\s*[^[:space:]]*\s*[^[:space:]]*\s*\)\(.\{10\}\).*\(.\{10\}.$\)/\1\2...\3/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment