Subfolders
Latest "Linux" files
Linux

Linux PHP tuning utilities & commands

1. See memory consumed by php-fpm8.2 (change this to match different PHP version if necessary)

ps --no-headers -o "rss,cmd" -C php-fpm8.2 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

Linux

Linux SQL Tuning Utilities

1. tuning-primer.sh

Run from Github:
curl -L https://raw.githubusercontent.com/BMDan/tuning-primer.sh/main/tuning-primer.sh | bash

2. MySQLTuner.pl

wget http://mysqltuner.pl/ -O mysqltuner.pl
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/basic_passwords.txt -O basic_passwords.txt
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/vulnerabilities.csv -O vulnerabilities.csv
perl mysqltuner.pl --host 127.0.0.1 --user [user] --pass [pass]

Remember to quote any punctuation or BASH tokens in the password.

Linux

Debian CPU spike process logger

This was a system service that ren every time the CPU spiked and logged what was running. Mostly it showed that php-fpm was what spiked the CPU. Fascinating.

daemon was at /etc/systemd/system/mk-cpu-watcher.service

[Unit]
Description=CPU Usage Watcher
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/mk-cpu-watcher.sh
Restart=always

[Install]
WantedBy=multi-user.target

script it ran was at /usr/local/bin/mk-cpu-watcher.sh


#!/bin/bash

LOG_FILE="/var/log/mk-cpu-spikes.log"
THRESHOLD=98 # Can set to threshold just below 100 to catch near-maximum usage

while true; do
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2+$4}' | cut -d. -f1)

if [ "$CPU_USAGE" -ge "$THRESHOLD" ]; then
echo "=== CPU spike detected at $(date) - Usage: ${CPU_USAGE}% ===" >> "$LOG_FILE"
echo "Top processes:" >> "$LOG_FILE"
ps -eo pid,ppid,cmd,%cpu,%mem --sort=-%cpu | head -11 >> "$LOG_FILE"
echo -e "\n" >> "$LOG_FILE"
fi

sleep 5 #…