bash 在 shell 脚本中按 Ctrl C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15156453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ctrl C in shell script
提问by Shane
I've got a shell script with the following code;
我有一个带有以下代码的 shell 脚本;
#!/bin/bash
nfcapd -z -w -t30 -p 2055 -l /home/shane/Documents/nfdump
My problem is that when I execute the shell script, the command above executes over and over (it captures network traffic from a router), but I'd like for it to stop after 30 seconds. In the terminal I would just press Ctrl+c, is there a way of executing this command (Ctrl+c) after a certain time t?
我的问题是,当我执行 shell 脚本时,上面的命令会一遍又一遍地执行(它从路由器捕获网络流量),但我希望它在 30 秒后停止。在终端中,我只需按Ctrl+ c,有没有办法在特定时间 t 后执行此命令 ( Ctrl+ c)?
回答by user000001
回答by Kaunteya
Put this statement after #!/bin/bash
把这句话放在后面 #!/bin/bash
(sleep 30 ; kill -9 $$ )&
(睡眠 30 ; 杀死 -9 $$ )&
See if it works in your case.
看看它是否适用于您的情况。
回答by Edouard Thiel
You may also kill the last job:
你也可以杀死最后一个工作:
#! /bin/bash
nfcapd -z -w -t30 -p 2055 -l /home/shane/Documents/nfdump &
sleep 30 ; kill %%
echo "nfcapd terminated"
exit 0

