[英]How do I stop execution of bash script?
I have script with a loop: 我有一个循环脚本:
until [ $n -eq 0 ]
do
tcpdump -i eth0 -c 1000000 -s 0 -vvv -w /root/nauvoicedump/`date "+%Y-%m-%d-%H-%M"`.pcap
n=$(($n-1))
echo $n 'times left'
done
I want to understand how to stop execution of this script? 我想了解如何停止执行此脚本?
<CTRL> + <C>
only stops the following iteration of the loop. <CTRL> + <C>
仅停止循环的后续迭代。
SIGINT
does not terminate the 'following iteration of the loop'. SIGINT
不会终止'循环的后续迭代'。 Rather, when you type ctrl-C, you are sending a SIGINT
to the currently running instance of tcpdump, and the loop continues after it terminates. 而是,当您键入ctrl-C时,您将
SIGINT
发送到当前正在运行的tcpdump实例,并在循环终止后继续循环。 A simple way to avoid this is to trap SIGINT
in the shell: 避免这种情况的一种简单方法是在shell中捕获
SIGINT
:
trap 'kill $!; exit' INT
until [ $n -eq 0 ]
do
tcpdump -i eth0 -c 1000000 -s 0 -vvv -w /root/nauvoicedump/`date "+%Y-%m-%d-%H-%M"`.pcap&
wait
n=$(($n-1))
echo $n 'times left'
done
Note that you need to run tcpdump in the background (append &
to the line that starts it) for this to work. 请注意,您需要在后台(附加运行tcpdump的
&
来启动它的线)这个工作。 If you have other background jobs running, you may need wait $!
如果您正在运行其他后台作业,则可能需要
wait $!
rather than just wait
. 而不是
wait
。
You should set the starting value of n at least 1 higher than 0. Example: 您应该将n的起始值设置为至少高于0的示例。示例:
n=100
until [ "$n" -eq 0 ]
...
It's also a good practice to quote your variables properly. 正确引用变量也是一种很好的做法。
Also it's probably better if you use a for loop: 如果你使用for循环也可能更好:
for (( n = 100; n--; )); do
tcpdump -i eth0 -c 1000000 -s 0 -vvv -w "/root/nauvoicedump/$(date '+%Y-%m-%d-%H-%M').pcap"
echo "$n times left"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.