Skip to content

Instantly share code, notes, and snippets.

@archisgore
Last active May 9, 2018 14:39
Show Gist options
  • Save archisgore/18b0a56dabd46f52fa5189ffbf9665bf to your computer and use it in GitHub Desktop.
Save archisgore/18b0a56dabd46f52fa5189ffbf9665bf to your computer and use it in GitHub Desktop.
For all processes with an command, run this command under their /proc/<pid>
function filter_pids() {
oifs="$IFS"
IFS=$'\n'
for stuff in $1; do
if [[ -f $stuff/cmdline ]]; then
echo "$stuff"
fi
done
IFS="$oifs"
}
function filter_cmd() {
oifs="$IFS"
IFS=$'\n'
for pid in $1; do
proc=$(cat "$pid/comm")
if [[ "$proc" == "$2" ]]; then
echo "$pid"
fi
done
IFS="$oifs"
}
function execute_rest() {
oifs="$IFS"
IFS=$'\n'
pids="$1"
shift
for pid in $pids; do
opwd="$PWD"
cd $pid
echo "------------------------------------------------------------------------------"
$@
echo "=============================================================================="
cd "$opwd"
done
IFS="$oifs"
}
echo "Running for all processes with command $1"
cmd="$1"
shift
procstuff=$(ls /proc)
pids=$(filter_pids "$procstuff")
match=$(filter_cmd "$pids" "$cmd")
execute_rest "$match" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment