【问题标题】:Clean up after calling nc in eternal bash loop在永恒的 bash 循环中调用 nc 后进行清理
【发布时间】:2014-03-04 11:21:55
【问题描述】:

我有一个最小的脚本,它只监听套接字并打印端口上显示的任何内容,当然,它工作正常。但是我',在我 ^z

之后无法清理
  • 也许 sigtstp 不是正确的方法?
  • 我可以让 Bash 听并按 ctrl+c 退出吗?
  • 我可以清理 nc 进程吗?它不断占领港口并迫使(嗯...)杀死-9?

#!/bin/bash
while :
do
    nc -l -p $1
done

【问题讨论】:

    标签: linux bash terminal debian


    【解决方案1】:

    我不知道“我可以让 bash 听 ^C 吗?”是什么意思,但试试这个脚本(未经测试,我没有安装 nc):

    #!/bin/bash
    pid=0
    
    cleanup() { (( $pid != 0 )) && kill -9 $pid; }
    trap "cleanup" EXIT
    
    while :
    do
       nc -l -p $1
       pid=$!
    done
    

    【讨论】:

    • Naa,对不起,这不会改变行为。我的意思是在 CTRL+c 上我希望脚本退出。
    • 你是说不能用 Ctrl+C 中止“while true ; do sleep 1s ; done”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 2020-12-03
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    相关资源
    最近更新 更多