链“ top”输出重定向和其他命令

时间:2018-11-28 18:47:44

标签: shell unix

我正在尝试在运行其他进程时使用top记录系统指标。我希望将事物链接在一起,就像这样:

#!/bin/bash

# Redirect `top` output
top -U $USER > file.txt &&
# Then run a process that just sleeps for 4 seconds
python3 -c 'import time;time.sleep(4)' &&
# Then run another process that does the same
python3 -c 'import time;time.sleep(4)'

但是,当我运行此命令时,后两个(Python)进程永远不会完成。我的目标是在其他任何进程开始之前从top开始记录,然后一旦这些进程完成,就从top停止记录。

1 个答案:

答案 0 :(得分:1)

#run the first command in background
top -U $USER > file.txt &
# Then run a process that just sleeps for 4 seconds
python3 -c 'import time;time.sleep(4)' &&
# Then run another process that does the same
python3 -c 'import time;time.sleep(4)' &&
# kill the command in background
kill %1