Skip to content

Instantly share code, notes, and snippets.

@aursu
Last active September 27, 2019 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aursu/7005b3dfddb70486a1a4c7a484938670 to your computer and use it in GitHub Desktop.
Save aursu/7005b3dfddb70486a1a4c7a484938670 to your computer and use it in GitHub Desktop.
Linux process monitoring with bash (project fail)
#!/bin/bash
QSIZE=5
MINLIFE=20
start=$(/usr/bin/date +%s)
btime=$(/usr/bin/cat /proc/stat | /usr/bin/awk '/btime/ {print $2}')
while /usr/bin/sleep 5; do
queues=$(/usr/sbin/rabbitmqctl list_queues | /usr/bin/grep "create-user-")
scripts=$(/usr/bin/ps aux | /usr/bin/grep rabbitmq:consumer | /usr/bin/grep -v grep)
for q in answerhub freshdesk magento phpbb; do
count=$(echo "$queues" | /usr/bin/awk -v qname="create-user-$q" '$1 == qname { print $2 }')
process=$(echo "$scripts" | /usr/bin/grep "create_user_$q" | /usr/bin/head -n 1 )
[ "$process" ] && {
pid=$(echo "$process" | /usr/bin/awk '{print $2}' )
[ -d "/proc/$pid" ] && {
startclocks=$(/usr/bin/cat /proc/$pid/stat | /usr/bin/awk '{print $22}')
hz=$(/usr/bin/perl -MPOSIX -e 'print POSIX::sysconf( &POSIX::_SC_CLK_TCK )')
starttime=$((startclocks/hz))
ctime=$(/usr/bin/date +%s)
uptime=$((ctime-btime))
lifetime=$((uptime-starttime))
echo $q:$pid:$lifetime
[ $count -ge $QSIZE ] && {
[ $lifetime -gt $MINLIFE ] && kill -9 $pid
}
}
}
done
end=$(/usr/bin/date +%s)
[ $(($end - $start)) -ge 55 ] && break;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment