Skip to content

Instantly share code, notes, and snippets.

@olivergondza
Last active August 29, 2015 14:12
Show Gist options
  • Save olivergondza/6627fbcb86c2f3df1b22 to your computer and use it in GitHub Desktop.
Save olivergondza/6627fbcb86c2f3df1b22 to your computer and use it in GitHub Desktop.
jps-monitor
#!/bin/sh
# Make periodic snapshots of running java processes.
#
# ./jps-monitor.sh jmap -heap
# ./jps-monitor.sh jmap -heap %s # PID of monitored process will be used for %s
target="jps-monitor/"
rm -rf $target
mkdir -p $target
cmd_pattern="$@"
while true; do
jps -lmvV | while read line ; do
# Skip jps itself
if grep -q sun.tools.jps.Jps <<< "$line"; then
continue;
fi
# Start logging new process
pid=`cut -d ' ' -f 1 <<< "$line"`
log="$target/$pid"
if [ ! -f $log ]; then
printf "$line\n" > $log
fi
cmd=`printf "$cmd_pattern" $pid`
if [ "$cmd" = "$cmd_pattern" ]; then
cmd="$cmd_pattern $pid"
fi
echo >> $log
date --rfc-3339=ns >> $log
$cmd >> $log 2>&1
sleep 1
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment