【问题标题】:Running a script with top command in the background在后台使用 top 命令运行脚本
【发布时间】:2019-10-03 12:25:20
【问题描述】:

我有一个脚本,基本上每秒将top -n1 的输出打印到一个文件中

最简单的形式:

while [ 1 ] ; do
   top -n1
   sleep 1
done

如果我像这样运行我的脚本:

./my_script.sh > out.log

运行良好

如果我在后台运行它:

./my_script.sh > out.log &

然后它给我Stopped(SIGTTOU) 错误。从其他 Q/As 我发现 top 正在尝试从标准输入中读取,并且在后台运行时没有标准输入。

如何将 top 作为后台任务记录到文件中?

【问题讨论】:

  • 你试过< /dev/null吗?
  • @melpomene 不,是在脚本里面吗? - 我不太确定将代码 sn-p 放在哪里? - 谢谢:)

标签: linux bash top-command


【解决方案1】:

您需要将顶部写入文件,并且在循环中..

#!/bin/bash
while [ 1 ] ; do
   top -b -n 1 > top.txt
   sleep 1
done

#!/bin/bash
while :
do
  top -b -n 1 > top.txt
  sleep 1
done

【讨论】:

  • 你的意思是#! /bin/bash :p
  • [ 1 ] 是写true 的奇怪方式。
  • @code_fodder 那为什么不直接使用:呢?不能比while :; do ... 更短。此外,true 实际上比[ 1 ] 短一个字符。
  • @melpomene 该死的...更好:))
  • 或相当于while true:until false; do stuff; done
猜你喜欢
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多