生产实践:
使用top命令打点记录应用占用系统资源使用情况
学习技巧:
Shell while循环,top命令使用
脚本内容:
使用top命令打点记录应用占用系统资源使用情况,用于追踪一段时间内应用内存泄漏,CPU占用过高等问题。
#!/bin/bash export COLUMNS=500 # 防止重写向写入日志内容不完整 LOGFILE="./top_monitor_$(date +%F).log" while true; do # 获取当前时间戳 timenow=$(date +%F\ %T) # 运行top命令并将输出追加到日志文件 echo -e "\n####################### ${timenow} #######################" >> ${LOGFILE} # 资源使用率排序 → 内存:%MEM,CPU:%CPU top -o %MEM -c -b -n 1 | head -20 >> ${LOGFILE} echo -e '\n========= Top5 %CPU =========' >> ${LOGFILE} ps auxw | { head -1;ps auxw|sort -rn -k3|head -5; } >> ${LOGFILE} echo -e '\n========= Top5 %MEM =========' >> ${LOGFILE} ps auxw | { head -1;ps auxf|sort -rn -k4|head -5; } >> ${LOGFILE} # 每分钟执行一次 sleep 60 done
查看效果:
####################### 2024-06-27 00:00:00 ####################### top - 00:19:05 up 570 days, 6:05, 1 user, load average: 0.62, 0.57, 0.41 Tasks: 148 total, 2 running, 145 sleeping, 0 stopped, 1 zombie %Cpu(s): 2.9 us, 2.9 sy, 0.0 ni, 94.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 3635.0 total, 170.2 free, 2590.6 used, 874.1 buff/cache MiB Swap: 0.0 total, 0.0 free, 0.0 used. 444.5 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2966281 mysql 20 0 2538948 582048 2104 S 0.0 15.6 10:44.62 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306 2458267 www 20 0 725024 138012 49444 S 0.0 3.7 1:56.92 php-fpm: pool www 2458274 www 20 0 720924 134056 49528 S 0.0 3.6 1:30.82 php-fpm: pool www 2458289 www 20 0 722868 132128 45760 S 0.0 3.5 2:16.96 php-fpm: pool www 2458292 www 20 0 720848 130372 45984 S 0.0 3.5 1:53.02 php-fpm: pool www 4118368 www 20 0 720136 126124 49884 S 0.0 3.4 0:19.40 php-fpm: pool www 4118365 www 20 0 714416 125316 47544 S 0.0 3.4 0:19.98 php-fpm: pool www 2289539 redis 20 0 480616 122400 1684 S 0.0 3.3 2682:55 /usr/local/redis/bin/redis-server 127.0.0.1:6379 1508034 www 20 0 708248 121084 49432 S 0.0 3.3 0:08.50 php-fpm: pool www 4118417 www 20 0 708184 117432 45668 S 0.0 3.2 0:21.06 php-fpm: pool www 2458283 www 20 0 704364 117148 49396 S 0.0 3.1 1:48.52 php-fpm: pool www 2458284 www 20 0 704504 115928 47872 S 0.0 3.1 1:21.28 php-fpm: pool www 4118363 www 20 0 704320 115252 47400 S 0.0 3.1 0:42.97 php-fpm: pool www ========= Top5 %CPU ========= USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND mysql 2966281 2.2 15.6 2538948 582048 ? Sl Jun26 10:44 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306 root 60 0.5 0.0 0 0 ? S 2022 4689:17 [kswapd0] root 2443806 0.5 2.4 463204 89888 ? Ss Jun24 18:25 /usr/lib/systemd/systemd-journald redis 2289539 0.4 3.2 480616 122400 ? Ssl 2023 2682:55 /usr/local/redis/bin/redis-server 127.0.0.1:6379 www 4118363 0.2 3.0 704320 115252 ? S Jun26 0:42 php-fpm: pool www ========= Top5 %MEM ========= USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND mysql 2966281 2.2 15.6 2538948 582048 ? Sl Jun26 10:44 \_ /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306 www 2458267 0.1 3.7 725024 138012 ? S Jun26 1:56 | \_ php-fpm: pool www www 2458274 0.1 3.6 720924 134056 ? S Jun26 1:30 | \_ php-fpm: pool www www 2458292 0.1 3.5 720848 130372 ? S Jun26 1:53 | \_ php-fpm: pool www www 2458289 0.1 3.5 722868 132128 ? S Jun26 2:16 | \_ php-fpm: pool www