Skip to content

Instantly share code, notes, and snippets.

@lancehudson
Last active April 8, 2016 20:55
Show Gist options
  • Save lancehudson/3250e7eff50f707ede345272a0f57286 to your computer and use it in GitHub Desktop.
Save lancehudson/3250e7eff50f707ede345272a0f57286 to your computer and use it in GitHub Desktop.
Kill stalled phantomjs processes
#!/bin/bash
# This script is created to handle buggy prerender where phantomjs hangs
PROCESS=phantomjs
LOGSERVER=local-hightailspaces.com
LOGSERVERPORT=6379
KEY=logstash
KEYTYPE=RPUSH
TYPE=phantomjs-monitor
KILL=false
logstash () {
if [ -z "${LOGSERVER}" ] ; then
echo "${1}"
else
(printf "%s %s \"%s\"\r\n" "${KEYTYPE}" "${KEY}" "{\\\"message\\\": \\\"${1}\\\", \\\"type\\\": \\\"${TYPE}\\\"}"; sleep 1) | nc ${LOGSERVER} ${LOGSERVERPORT} > /dev/null
fi
}
NUMBERPHANTOM=$(pgrep -c "${PROCESS}")
logstash "Number of ${PROCESS} processes ${NUMBERPHANTOM}"
while [[ $(pgrep -c "${PROCESS}") -gt 1 ]]
do
PID=$(pgrep -o "$PROCESS")
if [ "${KILL}" = true ] ; then
logstash "Killing oldest ${PROCESS} ${PID}"
pkill -o "${PROCESS}"
else
logstash "TEST: Killing oldest ${PROCESS} ${PID}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment