我有一个非常简单的Bash脚本,它以批处理模式运行top
x秒,将输出保存到文件,并在一段时间后自动杀死top
#!/bin/bash
# Runs top under batch mode with renewal period set to $1, saves the data to file $2, and kills the process after $3 minutes
top -b -d $1 > $2 &
PROC=$!
(sleep $3m; kill $PROC) &
问题是,我也希望能够处理脚本进程的杀戮,就像这样(我更喜欢这个超过杀戮)
ctrl_c()
# run if user hits control-c
{
echo -en "\n*** Done ***\n"
cleanup # some cleanup function
kill $PROC
}
# trap keyboard interrupt (control-c)
trap ctrl_c SIGINT
while true; do read x; sleep $1; done
有更简洁的方法吗?
答案 0 :(得分:1)
为什么不给top
多次迭代来运行:
(( total_runtime = $3 * 60 ))
(( iterations = total_runtime / $1 ))
top -b -d $1 -n ${iterations}