Skip to content

Instantly share code, notes, and snippets.

@tangoslee
Created August 27, 2020 18:06
Show Gist options
  • Select an option

  • Save tangoslee/1be8e1414ae708bc83c4d023fcc4f943 to your computer and use it in GitHub Desktop.

Select an option

Save tangoslee/1be8e1414ae708bc83c4d023fcc4f943 to your computer and use it in GitHub Desktop.
Average Memory Usage of some process
#!/bin/bash
#ps --no-headers -o "rss,cmd" -C php-fpm7.4 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"MB") }'
function chk() {
name="$1"
val=$(ps --no-headers -o "rss,cmd" -C "$name" | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"MB") }')
printf " %10s |" $val
}
function header() {
printf "| %-24s | %10s | %10s | %10s | %10s |" "Date" "Node" "Nginx" "PHP" "PHP-FPM"
echo ""
}
function body() {
printf "| %24s |" $now
chk node
chk nginx
chk php
chk php-fpm7.4
echo ""
}
LOG_FILE="/var/log/proc_mem_$(date +%Y%m%d).log"
now=$(date +%Y-%m-%dT%T%z)
if [ "$1" = "--log" ];then
LOG=1
else
LOG=0
fi
if [ $LOG -eq 0 ]; then
header
body
else
[ -f "$LOG_FILE" ] || header > "$LOG_FILE"
body >> "$LOG_FILE"
fi
@tangoslee
Copy link
Author

  • Output
| Date                     |       Node |      Nginx |        PHP |    PHP-FPM |
| 2020-08-27T18:06:29+0000 |      144MB |       11MB |       53MB |       88MB |

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