Skip to content

Instantly share code, notes, and snippets.

@aitseitz
Last active September 29, 2023 16:24
Show Gist options
  • Save aitseitz/d51f8f796aaf782b1c2358f45815b5cd to your computer and use it in GitHub Desktop.
Save aitseitz/d51f8f796aaf782b1c2358f45815b5cd to your computer and use it in GitHub Desktop.
ACS threaddump_script.sh
#!/bin/bash
# This Script creates threaddumps
# for Alfresco Content Service (ACS) and Alfresco Search Services (ASS)
# in specified log directory
# Script could be used in a cronjob, therefore a delete option for old
# threaddumps is implemented
# Variables:
JAVA_HOME="/opt/java/current"
LOG_DIR="/opt/alfresco/logs"
# Systemd Services for ACS & ASS
SERVICES="acs.service ass.service"
THREAD_DUMP_INTERVAL=20
THREAD_DUMP_COUNT=5
TIMESTAMP=`date +%Y-%m-%d-%H%M`
RETENTION_TIME_MINUTES=120
echo "************************"
echo "* threaddump_script.sh *"
echo "************************"
echo "Automatically creates Threaddumps every ${THREAD_DUMP_INTERVAL} sec for ${THREAD_DUMP_COUNT} times in a row"
echo "----"
for SERVICE in ${SERVICES}
do
# Get Main PID vom the Java Process via systemd service
MAIN_PID=`systemctl show --property MainPID --value ${SERVICE}`
USER_ID=`systemctl show --property User --value ${SERVICE}`
# alternative jps -l can be used (but output is not that pretty)
echo "${SERVICE} --> PID=${MAIN_PID}"
if [ ${MAIN_PID} != 0 ];
then
for ((i=1;i<=${THREAD_DUMP_COUNT}; i++ ))
do
# Method jstack
# echo "Creating jtack threaddump for ${SERVICE}(${MAIN_PID})"
# ${JAVA_HOME}/bin/jstack -l ${MAIN_PID} >> ${LOG_DIR}/threaddump_${SERVICE}_${MAIN_PID}_${TIMESTAMP}.out | sudo su - ${USER_ID}
# Method jcmd (at least jdk 8)
echo "Creating jcmd threaddump for ${SERVICE}(${MAIN_PID})"
# sidenote:
# Cause we have no root permissions, we need to run the threaddump command as the specific user who runs the java process
echo "${JAVA_HOME}/bin/jcmd ${MAIN_PID} Thread.print >> ${LOG_DIR}/threaddump_${SERVICE}_${MAIN_PID}_${TIMESTAMP}.out" | sudo su - ${USER_ID}
sleep ${THREAD_DUMP_INTERVAL}
if [ $i == ${THREAD_DUMP_COUNT} ];
then
echo "----"
fi
done
fi
done
# Cleanup Option:
# search for threaddump files older then x and delete them
#find ${LOG_DIR} -type f -name "threaddump_*" -mtime ${RETENTION_TIME} -exec echo rm -f {} \;
echo ""
echo "Cleanup - Check for threaddumps older then ${RETENTION_TIME_MINUTES} Minutes"
find ${LOG_DIR} -type f -name "threaddump_*" -mmin +${RETENTION_TIME_MINUTES} -exec rm -vf {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment