【问题标题】:Bash script output of a top commandtop 命令的 Bash 脚本输出
【发布时间】:2017-05-29 09:18:39
【问题描述】:

当我执行命令时

top -c -b -n | grep XXX > TEST

在 Red Hat Linux 命令行上,top 命令中的所有路径和其余内容都被导出到TEST 文件中(例如,在一行中它写了超过 100 列/字符)。

但是,当我在脚本中添加相同的命令并通过crontab 运行它时,每次只输出到第 81 列/字符。

如何获取cron 脚本中的全宽信息?

【问题讨论】:

    标签: bash shell file-io top-command


    【解决方案1】:

    解释在man top的第1节:

       -w  :Output-width-override as:  -w [ number ]
            In  Batch  mode, when used without an argument top will
            format output using the COLUMNS= and LINES= environment
            variables,  if  set.  Otherwise, width will be fixed at
            the maximum 512  columns.   With  an  argument,  output
            width can be decreased or increased (up to 512) but the
            number of rows is considered unlimited.
    
            In normal display mode, when used without  an  argument
            top  will  attempt  to format output using the COLUMNS=
            and LINES= environment  variables,  if  set.   With  an
            argument,  output  width  can  only  be  decreased, not
            increased.  Whether using environment variables  or  an
            argument  with -w, when not in Batch mode actual termi‐
            nal dimensions can never be exceeded.
    
            Note: Without the use of this command-line option, out‐
            put  width is always based on the terminal at which top
            was invoked whether or not in Batch mode.
    

    COLUMNSLINES 在您启动终端和调整其窗口大小时由终端设置。

    【讨论】:

      【解决方案2】:

      您可以尝试以下方法:

      COLUMNS=1000 top -c -b -n 1  | grep XXX > TEST
      

      这将确保 COLUMNS 环境变量值设置为较大的值。

      礼貌:ServerFault

      【讨论】:

      • 非常感谢您的回答,这真的很有帮助。 Igor Kuzmitshov 它就像一个魅力。
      猜你喜欢
      • 2014-10-30
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 2019-01-14
      • 2015-06-17
      • 1970-01-01
      相关资源
      最近更新 更多