Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
「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
You can’t perform that action at this time.