面试题答案
一键面试#!/bin/bash
log_file="/var/log/cpu_usage.log"
while true; do
cpu_usage=$(top -bn1 | grep '%Cpu' | awk '{print $2}')
echo "$(date +'%Y-%m-%d %H:%M:%S') CPU使用率: $cpu_usage%" >> $log_file
sleep 300
done
#!/bin/bash
log_file="/var/log/cpu_usage.log"
while true; do
cpu_usage=$(top -bn1 | grep '%Cpu' | awk '{print $2}')
echo "$(date +'%Y-%m-%d %H:%M:%S') CPU使用率: $cpu_usage%" >> $log_file
sleep 300
done