为了研究我的 Mac 系统活动,我希望直观地看到每天的正常运行时间、睡眠时间和 CPU 平均值。
我的第一想法是查看系统日志,但我想知道当前有哪些工具可用。我见过的大多数工具都只与当前使用情况有关,但我只想查看每日摘要。
答案1
last
显示计算机关闭或重新启动的时间,但不显示计算机进入睡眠状态或唤醒的时间:
$ last|head
lauri ttys002 Fri Dec 13 11:56 still logged in
lauri ttys003 Fri Dec 13 11:55 - 11:56 (00:00)
lauri ttys002 Fri Dec 13 11:55 - 11:56 (00:00)
lauri ttys002 Fri Dec 13 11:16 - 11:55 (00:38)
lauri ttys003 Fri Dec 13 11:16 - 11:55 (00:38)
lauri console Fri Dec 13 10:08 still logged in
reboot ~ Fri Dec 13 10:06
shutdown ~ Fri Dec 13 10:05
lauri console Fri Dec 13 08:27 - 10:05 (01:38)
reboot ~ Fri Dec 13 08:25
pmset -g log
还显示计算机何时进入睡眠或唤醒,包括每个睡眠阶段的持续时间:
$ pmset -g log|awk '$5~/Sleep|Wake/'|head
11/30/13, 6:07:11 AM GMT Sleep Software Sleep pid=45: Using AC 16949 secs
11/30/13, 10:49:40 AM GM Wake Wake [CDNVA] due to XHC1/HID Activity: Using AC
Sleep/Wakes since boot:1 Dark Wake Count in this sleep cycle:0
12/1/13, 2:31:19 AM GMT+ Sleep Software Sleep pid=45: Using AC
12/1/13, 3:29:10 AM GMT+ Wake Wake [CDNVA] due to XHC1/HID Activity: Using AC 120 secs
Sleep/Wakes since boot:2 Dark Wake Count in this sleep cycle:0
12/1/13, 3:31:10 AM GMT+ Sleep Software Sleep pid=45 to DarkWake: Using AC
12/1/13, 3:33:14 AM GMT+ Sleep Software Sleep pid=45: Using AC 50927 secs
12/1/13, 5:42:01 PM GMT+ DarkWake DarkWake [CDN] due to XHC1/: Using AC 7 secs
12/1/13, 5:42:08 PM GMT+ Wake DarkWake to FullWake [CDNVA] due to HID Activity: Using AC
要测量平均 CPU 使用率,您可以向 crontab 添加如下一行:
*/5 * * * * ps -eo \%cpu=|awk '{s+=$0}END{print s}'>>~/Documents/cpu-$(date +\%F)
要查看哪些进程使用了最多的 CPU,请在 crontab 中添加如下一行:
* * * * * ps -eco '\%cpu= comm='|sed $'s/^ *//;s/ /\t/'>>~/Documents/cpu
然后稍后运行如下命令:
awk -F$'\t' '{a[$2]+=$1}END{for(i in a)print a[i],i}' ~/Documents/cpu|sort -rn