Last active
May 6, 2017 17:18
-
-
Save jrelo/e9ee72cd0602ac1e72ae665f12ac64a4 to your computer and use it in GitHub Desktop.
watch a log with tail -f and a fifo to run command when a grep match is made
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#fifo_logwatch.sh -jrelo | |
if [ $# -lt 1 ]; | |
then | |
printf "Usage: $0 <logfile>\n" $# | |
exit 0 | |
fi | |
trap ctrl_c INT | |
fifo=/tmp/fifolog.$$ | |
mkfifo "${fifo}" || exit 1 | |
logfile=$1 | |
function ctrl_c() { | |
echo "ctrl^c caught. exiting..." | |
rm "${fifo}" | |
exit 130; | |
} | |
while sleep 1 | |
do | |
tail -fn0 ${logfile} >${fifo} & | |
tailpid=$! | |
grep -m 1 "Too many open files" "${fifo}" | |
if [[ $? -eq 0 ]]; then | |
ulimit -n;sysctl fs.file-nr ; sysctl fs.file-max ; lsof -n|awk '{print $1}'|sort|uniq -c|sort -n | |
fi | |
done | |
#kill "${tailpid}" #uncomment if you want it to die after first match | |
rm "${fifo}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment