0

我的笔记本电脑经常被加热到 60-75 摄氏度左右的高温。我已经安装了lm_sensors。而且我发现很少有命令可以做到这一点。我需要生成一个文件,其中每隔 1 分钟测量一次参数。

  1. 温度测量值。可以通过命令完成sensors >> temperature.txt
  2. 测量温度的时间。它可以通过date >> temperature.txt
  3. 当前运行的进程数ps-aux(但这不会给出进程)。

我开始知道这是一项需要完成的任务shell scripts(是吗?)。任何人都可以建议我这样做,因为我对 shell 脚本一无所知吗?

4

1 回答 1

1

脚本本身可能类似于:

#!/bin/bash

file=/your_home_dir/temp_info
temperature=$(sensors | tail -3)
when=$(date "+%Y%m%d_%H%M%S")
working_proc=$(ps -aux | wc -l)

echo "$when num_proc: $working_proc" >> $file
echo "$temperature" >> $file

输出像

20130724_131150 num_proc: XXX
temp line1
temp line2
20130724_131250 num_proc: YYY
temp line1
temp line2
...

要每 1 分钟计算一次,您可以使用crontab

执行crontab -e并添加以下行:

* * * * * /bin/sh /path/to/script.sh 2>/dev/null

您可以在https://stackoverflow.com/tags/crontab/info中查看有关 crontab 的更多信息。基本思路:

  • 使用此表达式* * * * *crontab每分钟、任何一天、任何时间都运行。
  • 我们避免了2>/dev/null假设的错误消息出现在控制台中。它们是“有针对性的”,/dev/null因此它们不会出现。想法:做ls -l /hellooooo,你会得到一个错误信息。做ls -l /helloooo 2>/dev/null,你不会。
于 2013-07-23T09:49:49.323 回答