Cron 表达式:
# m h dom mon dow 命令
*/1 * * * */home/sysadmin/sample.sh
Shell脚本内容:
#!/bin/bash
clear
Vardate=`date`
topresult=`top -n 1| grep Cpu`
# CPUStatus=$Vardate"\t"$topresult
# echo $CPUStatus >> /tmp/data.log
echo $Vardate" "$topresult >> /tmp/data.log
当我手动执行脚本时,我得到了所需的输出。这是:
Wed Sep 23 02:42:47 MDT 2015 Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
但是当cronjob执行时,输出如下:
Wed Sep 23 02:42:47 MDT 2015
这可能是什么原因?
最佳答案
使用top的“批处理”模式。
替换:
topresult=`top -n 1| grep Cpu`
与:
topresult=`top -b -n 1| grep Cpu`
-b : Batch mode operation Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.
关于linux - Shell 脚本(使用 top 命令)在 cronjob 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32735860/