Skip to content

Instantly share code, notes, and snippets.

@niedbalski
Last active September 14, 2017 20:29
Show Gist options
  • Save niedbalski/0cfa4a3c91839ea9d124a3659befff41 to your computer and use it in GitHub Desktop.
Save niedbalski/0cfa4a3c91839ea9d124a3659befff41 to your computer and use it in GitHub Desktop.
strace-hook.sh
#!/bin/bash
# ./strace-hook.sh update-status
# this command straces all the processes and subprocesses spawned
# by a particular juju hook when is run.
set -e
hook=${1:-update-status}
regex=".*${hook}.*"
trace_dir=${2:-trace}
if [ ! -e ${trace_dir} ]; then
mkdir ${trace_dir}
fi
function ctrl_c() {
killall -9 strace && exit 0;
}
function monitor() {
while true; do
pids=""
for f in $(grep -irl ${regex} /proc/*/cmdline | grep -v self); do
pids+=$(echo $(basename $(dirname $f)) | awk '{print" -p "$1}')
done
if [ -n "$pids" ]; then
strace $pids -ff -t -o ${trace_dir}/strace
fi
sleep .5
done
}
trap ctrl_c INT
monitor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment