Skip to content

Instantly share code, notes, and snippets.

@matteomattei
Last active October 9, 2018 19:02
Show Gist options
  • Save matteomattei/d2335a3ee5d13d0d1acb285806624ea9 to your computer and use it in GitHub Desktop.
Save matteomattei/d2335a3ee5d13d0d1acb285806624ea9 to your computer and use it in GitHub Desktop.
This script calculates the MaxClients directive in Apache MPM_prefork module.
#!/bin/bash
TOTAL_MEMORY=$(grep "^MemTotal:" /proc/meminfo | awk '{print $2}')
MYSQL_MEMORY=$(ps aux | grep mysql | grep -v "grep" | grep "^mysql" | awk '{print $6}' | sort | head -n 1)
APACHE_MEMORY_PROCESSES=$(ps aux | grep 'apache' | grep -v "grep" | awk '{print $6}' | sort)
APACHE_INSTANCES=0
APACHE_TOTAL_MEMORY=0
for i in ${APACHE_MEMORY_PROCESSES}
do
APACHE_TOTAL_MEMORY=$(( ${APACHE_TOTAL_MEMORY} + ${i} ))
APACHE_INSTANCES=$(( ${APACHE_INSTANCES} + 1 ))
done
APACHE_MEMORY=$(( ${APACHE_TOTAL_MEMORY} / ${APACHE_INSTANCES} ))
TOTAL_MEMORY_MB=$(( ${TOTAL_MEMORY} / 1024 ))
MYSQL_MEMORY_MB=$(( ${MYSQL_MEMORY} / 1024 ))
APACHE_MEMORY_MB=$(( ${APACHE_MEMORY} / 1024 ))
LEFT_MEMORY=$(( ${TOTAL_MEMORY_MB} - ${MYSQL_MEMORY_MB} - 50 ))
MAX_CLIENT=$(( ${LEFT_MEMORY} / ${APACHE_MEMORY_MB} ))
echo "MaxClients=${MAX_CLIENT}"
@apmuthu
Copy link

apmuthu commented Jan 12, 2017

The last line:
echo "MaxClients="${MAX_CLIENT}"
should be:
echo "MaxClients=${MAX_CLIENT}"

@matteomattei
Copy link
Author

Thanks, fixed! ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment