脚本本身可能类似于:
#!/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
,你不会。