Cron expression:
Cron表达式:
# m h dom mon dow command */1 * * * * /home/sysadmin/sample.sh
我的命令是*/1 * * * * */ home/sysadmin/sample.sh
Shell Script content:
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
When i exeute the script manually, I get the desired output. Which is:
当我手工执行脚本时,我将得到所需的输出。这是:
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
But when the cronjob executes it, the output is as follows:
但是当cronjob执行时,输出如下:
Wed Sep 23 02:42:47 MDT 2015
What can be the reason for this?
为什么会这样?
2 个解决方案
#1
2
Use top's "batch" mode.
使用前的“批处理”模式。
replace:
替换:
topresult=`top -n 1| grep Cpu`
with:
:
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.
-b:批处理模式操作以“批处理模式”开始,这对于将输出从顶部发送到其他程序或文件很有用。在这种模式下,top将不接受输入并运行,直到您使用“-n”命令行选项设置的迭代限制或终止。
Source: http://linux.die.net/man/1/top
来源:http://linux.die.net/man/1/top
#2
0
You can use this script:
您可以使用这个脚本:
export TERM=xterm
FILE_NAME=top-$(date +%Y%m%d).log
top -b -n 1 >> /home/ec2-user/$FILE_NAME
#1
2
Use top's "batch" mode.
使用前的“批处理”模式。
replace:
替换:
topresult=`top -n 1| grep Cpu`
with:
:
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.
-b:批处理模式操作以“批处理模式”开始,这对于将输出从顶部发送到其他程序或文件很有用。在这种模式下,top将不接受输入并运行,直到您使用“-n”命令行选项设置的迭代限制或终止。
Source: http://linux.die.net/man/1/top
来源:http://linux.die.net/man/1/top
#2
0
You can use this script:
您可以使用这个脚本:
export TERM=xterm
FILE_NAME=top-$(date +%Y%m%d).log
top -b -n 1 >> /home/ec2-user/$FILE_NAME