Skip to content

Instantly share code, notes, and snippets.

@usiusi360
Last active June 27, 2019 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save usiusi360/24c775f1674b6eda31b56adef725b9f3 to your computer and use it in GitHub Desktop.
Save usiusi360/24c775f1674b6eda31b56adef725b9f3 to your computer and use it in GitHub Desktop.
「yum ps all」と同様の結果を出力するスクリプト
#!/bin/bash
OS_FLAG=0
which rpm >/dev/null 2>&1 && OS_FLAG=1
which dpkg >/dev/null 2>&1 && OS_FLAG=2
if [ ${OS_FLAG} == "0" ]; then
echo "Unknown OS"; exit 1
fi
if [ ${OS_FLAG} == "2" ]; then
apt-file update >/dev/null 2>&1 || exit 1
fi
IFS=$'\n'
PIDS=`ps --no-headers --ppid 2 -p 2 --deselect -o pid,comm | awk '{print $1,$2}'`
for line in ${PIDS}; do
pid=`echo "${line}" | cut -d " " -f1`
PROCS+=("${line} `ls -l /proc/${pid}/exe 2>/dev/null | awk '{print $11}'`")
MAPS=`cat /proc/${pid}/maps 2>/dev/null | grep -v " 00:00 " | awk '{print $6}' | sort -n | uniq`
for map in ${MAPS}; do
PROCS+=("${line} ${map}")
done
done
PROCS_SHAPE=(`echo "${PROCS[*]}" | sort -n | uniq | grep -v /var/log`)
for line in ${PROCS_SHAPE[@]}; do
pid=`echo "$line" | cut -d " " -f1`
comm=`echo "$line" | cut -d " " -f2`
path=`echo "$line" | cut -d " " -f3`
if [ ${OS_FLAG} == "1" ]; then
pkg_name=`rpm -qf ${path} 2>/dev/null` || continue
else
pkg_name=`dpkg -S ${path} 2>/dev/null` && pkg_name=`echo ${pkg_name}| cut -d ":" -f1` || continue
fi
PKGS+=("${pkg_name} ${pid} ${comm}")
done
echo "${PKGS[*]}" | sort -n | uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment