I have a small script do count open files on Linux an save results into a flat file. I intend to run it on Cron every minute to gather results later. Script follows:
我有一个小脚本在Linux上计算打开文件,将结果保存到平面文件中。我打算每分钟在Cron上运行它以便稍后收集结果。脚本如下:
/bin/echo "Timestamp: ` date +"%m-%d-%y %T"` Files: `lsof | grep app | wc -l`"
And the crontab is this:
而crontab是这样的:
*/1 * * * * /usr/local/monitor/appmon.sh >> /usr/local/monitor/app_stat.txt
If I run from shell ./script.sh it works well and outputs as:
如果我从shell ./script.sh运行它运行良好并输出为:
Timestamp: 01-31-13 09:33:59 Files: 57
But on the Cron output is:
但是在Cron输出上是:
Timestamp: 01-31-13 09:33:59 Files: 0
Not sure if any permissions are needed or similar. I have tried with sudo on lsof without luck as well.
不确定是否需要任何权限或类似。我已经尝试过sus on lsof也没有运气。
Any hints?
任何提示?
1 个解决方案
#1
10
from your working cmd-line, do
从你的工作cmd-line,做
which lsof
which grep
which wc
which date
Take the full paths for each of these commands and add them into your shell script, producing something like
获取每个命令的完整路径并将它们添加到shell脚本中,生成类似的内容
/bin/echo "Timestamp: `/bin/date +"%m-%d-%y %T"` Files: `/usr/sbin/lsof | /bin/grep app | /bin/wc -l`"
OR you can set a PATH var to include the missing values in your script, i.e.
或者您可以设置PATH var以在脚本中包含缺失值,即
PATH=/usr/sbin:${PATH}
Also unless you expect your script to be run from a true Bourne Shell environment, join the early 90's and use the form $( cmd ... )
for cmd-substitution, rather than backticks. The Ksh 93 book, published in 1995 remarks that backticks for command substitution are deprecated ;-)
除非您希望您的脚本从真正的Bourne Shell环境运行,否则请加入90年代早期并使用$(cmd ...)格式进行cmd替换,而不是使用反引号。 1995年出版的Ksh 93书评论说,命令替换的反引号已被弃用;-)
IHTH
IHTH
#1
10
from your working cmd-line, do
从你的工作cmd-line,做
which lsof
which grep
which wc
which date
Take the full paths for each of these commands and add them into your shell script, producing something like
获取每个命令的完整路径并将它们添加到shell脚本中,生成类似的内容
/bin/echo "Timestamp: `/bin/date +"%m-%d-%y %T"` Files: `/usr/sbin/lsof | /bin/grep app | /bin/wc -l`"
OR you can set a PATH var to include the missing values in your script, i.e.
或者您可以设置PATH var以在脚本中包含缺失值,即
PATH=/usr/sbin:${PATH}
Also unless you expect your script to be run from a true Bourne Shell environment, join the early 90's and use the form $( cmd ... )
for cmd-substitution, rather than backticks. The Ksh 93 book, published in 1995 remarks that backticks for command substitution are deprecated ;-)
除非您希望您的脚本从真正的Bourne Shell环境运行,否则请加入90年代早期并使用$(cmd ...)格式进行cmd替换,而不是使用反引号。 1995年出版的Ksh 93书评论说,命令替换的反引号已被弃用;-)
IHTH
IHTH