从cron运行时,顶部命令输出为空 [英] Top Command Output is Empty when run from cron

查看:237
本文介绍了从cron运行时,顶部命令输出为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用以下命令每5分钟重定向特定文件中的TOP命令输出。

I was trying to redirect the TOP command output in the particular file in every 5 minutes with the below command.

top -b -n 1 > /var/tmp/TOP_USAGE.csv.$(date +"%I-%M-%p_%d-%m-%Y")

-rw-r--r--   1 root root 0 Dec  9 17:20 TOP_USAGE.csv.05-20-PM_09-12-2015
-rw-r--r--   1 root root 0 Dec  9 17:25 TOP_USAGE.csv.05-25-PM_09-12-2015
-rw-r--r--   1 root root 0 Dec  9 17:30 TOP_USAGE.csv.05-30-PM_09-12-2015
-rw-r--r--   1 root root 0 Dec  9 17:35 TOP_USAGE.csv.05-35-PM_09-12-2015

因此我做了一个非常小的(1行)shell脚本,所以我可以运行在每5分钟通过cronjob。

Hence i made a very small (1 line) shell script for this, so that i can run in every 5 minutes via cronjob.

问题是当我手动运行这个脚本,然后我可以看到文件中的输出,但是当这个脚本在自动运行时,文件是每5分钟生成一次,但有没有数据(aka文件为空)

Problem is when i run this script manually then i can see the output in the file, however when this script in running automatically, file is generating in every 5 minutes but there is no data (aka file is empty)

任何人都可以帮我解决这个问题吗?

Can anyone please help me on this?

我现在修改了脚本,但仍然是一样的。

I now modified the script and still it's the same.

#!/bin/sh
PATH=$(/usr/bin/getconf PATH)

/usr/bin/top -b -n 1 > /var/tmp/TOP_USAGE.csv.$(date +"%I-%M-%p_%d-%m-%Y")


推荐答案

很可能从cron传递给脚本的环境太小了。特别是 PATH 可能不是你认为的(没有配置文件是从cron启动的脚本读取的)。

Most likely the environment passed to your script from cron is too minimal. In particular, PATH may not be what you think it is (no profiles are read by scripts started from cron).

在脚本开始处放置 PATH = $(/ usr / bin / getconf PATH),然后使用

Place PATH=$(/usr/bin/getconf PATH) at the start of your script, then run it with

/usr/bin/env -i /path/to/script


b $ b

一旦没有错误,它就可以用于cron。

Once that works without error, it's ready for cron.

这篇关于从cron运行时,顶部命令输出为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆