Skip to content

Instantly share code, notes, and snippets.

@svalaskevicius
Created December 12, 2012 18:03
Show Gist options
  • Save svalaskevicius/4270094 to your computer and use it in GitHub Desktop.
Save svalaskevicius/4270094 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
if [[ $1 == "-v" ]]; then
verbose=1
shift
fi
process=$1
main() {
enforce_usage
attache_strace
}
enforce_usage() {
if [ -z "$process" ]; then
usage
exit $E_BADARGS
fi
}
usage() {
echo "Attach strace to all occurrences of a process"
echo "usage: `basename $0` process"
}
attache_strace() {
for p in $(ps aux | grep $process | grep -v grep | awk '{print $2}') ; do
strace -s1024 -rp$p 2>&1 | tee ~/strace_$p.log >&2 &
echo "strace ${process} {$p} attached!"
done
echo "Ctrl+C to stop"
trap 'echo "quitting..." ; detach_strace' SIGINT
spinner $$ &
while :; do
sleep 1
if test 1 -gt "$(jobs -l | wc -l)" ; then
echo "no more processes left"
exit 0;
fi
for p in $(ps aux | grep $process | grep -v strace | grep -v grep | awk '{print $2}') ; do
if ! test -f ~/strace_$p.log ; then
strace -s1024 -rp$p 2>&1 | tee ~/strace_$p.log >&2 &
echo "strace ${process} {$p} attached!"
fi
done
done
}
detach_strace() {
for p in $(jobs -l | grep 'strace -s' | awk '{print $2}' ) ; do
kill $p
done
}
log_command() {
debug "=> $*"
eval $*
}
debug() {
if [ $verbose ]; then
echo "$*" >&2
fi
}
spinner()
{
local pid=$1
local delay=0.1
local spinstr="/-\|"
while ps p $pid >/dev/null ; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment