0

My system gets me an error that saying 'Too many open files'. I investigated this error and turned out that /usr/bin/uwsgi created sockets(?) more than 1020. If it creates more than 1020, I guess, the above error comes up with.

So What I try to do is to run a shell script that monitors the number of open files and if it exceeds more than 1000, to kill its PID to resolve this error at this stage.

#uwsgimonitor.sh
#!/bin/bash

#filename=/usr/bin/uwsgi

filename=/usr/bin/uwsgi
Cnt= lsof | awk '/uwsgi/ {print $1,$2}' | uniq -c | sort -r | head -1 | awk '{print $1}'
PROCESS_ID = lsof | awk '/uwsgi/ {print $1,$2}' | uniq -c | sort -r | head -1 | awk '{print $2}'

if [ $Cnt gt 1000 ]
then
    echo "Found the number of socket open exceeds $Cnt."
    kill -9 $PROCESS_ID
else
    echo "" #nothing to do.
fi

In crontab, I added this line but it seems not to kill the PID that I meant.

* * * * * sh /home/root/scripts/uwsgimonitor.sh

What am I missing?

Thank you in advance.

deokyong song
  • 13
  • 1
  • 3
  • This won't really solve your problem - you need to look at how to configure UWSGI/your webserver to limit the number of sockets opened, or allow the user it runs as to open more files via ulimits.conf. – shearn89 Feb 14 '22 at 11:50
  • I should've mentioned the background. I am an automation tester and this issue on the track fixed. Meanwhile, I just use this shell script to prevent any error message from popping up. It is just a temporary solution to test my app. – deokyong song Feb 15 '22 at 22:30

1 Answers1

0

For those who want to how my situation went.

#!/bin/bash


count=$(lsof | awk '/uwsgi/ {print $1,$2}' | uniq -c | sort -r | head -1 | awk '{print $1}')
PROCESS_ID=$(lsof | awk '/uwsgi/ {print $1,$2}' | uniq -c | sort -r | head -1 | awk '{print $2}')
expectLimit=1000

echo "open socket:$count"
echo "pid:$PROCESS_ID"

if [[ ${count} -gt ${expectLimit} ]]
then
        echo "Found uwsgi exceeds limit :${expectLimit}"
        kill -9 $PROCESS_ID
else
        echo "" #nothing to do.
fi

This one works for me. Thanks.

deokyong song
  • 13
  • 1
  • 3