linux - Shell 脚本(使用 top 命令)在 cronjob 中不起作用

标签 linux bash shell cron

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.

来源:http://linux.die.net/man/1/top

关于linux - Shell 脚本(使用 top 命令)在 cronjob 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32735860/

相关文章:

shell - zsh 提示和主机名

linux - RAM如何分配?

linux - 从 awk 脚本内的变量字符串中删除最后一行

linux - Linux访问另一个进程虚拟内存(调试)

linux - 在 Linux 内核中存储信息的最佳方式是什么?

bash - 如果我将脚本作为后台进程,会发生什么情况?

linux - 自定义 shell 的 shebang?

linux - 为新的 Hadoop 集群自动配置 SSH

C、Bash - 运行 bash 脚本并在同一文件上并行工作的进程

linux - 如何为谷歌云实例设置变量名