AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1099132
Accepted
Janith
Janith
Asked: 2018-12-08 01:39:45 +0800 CST2018-12-08 01:39:45 +0800 CST 2018-12-08 01:39:45 +0800 CST

为管道分配的变量不起作用

  • 772

我有一个 shell 脚本来检查端口状态。检查端口是否繁忙后,将杀死它。

#check port and kill
checkPort(){
     check=$(sudo netstat -nlpt | grep 2020 | awk '{print $6}')
    word="LISTEN"
    killProcess=$(sudo netstat -nlpt | grep 2020 | awk '{print $7}' |
                  sed 's/.\{5\}$//' | sort -u |  xargs -t kill -9)
    if [[ "$check" == "$word"  ]];
    then
        echo "killing the port 2020"
        $killProcess
     else
        echo "Not Listen"
    fi
}

当我运行$killProcess变量时,我遇到了错误。但是,如果我将完整的命令sudo netstat -nlpt | grep 2020 | awk '{print $7}' | sed 's/.\{5\}$//' | sort -u | xargs -t kill -9放在if条件内,它就可以正常工作。这里会有什么错误?

我得到的错误:

kill -9 

Usage:
 kill [options] <pid|name> [...]

Options:
 -a, --all              do not restrict the name-to-pid conversion to processes
                        with the same uid as the present process
 -s, --signal <sig>     send specified signal
 -q, --queue <sig>      use sigqueue(2) rather than kill(2)
 -p, --pid              print pids without signaling them
 -l, --list [=<signal>] list signal names, or convert one to a name
 -L, --table            list signal names and numbers

 -h, --help     display this help and exit
 -V, --version  output version information and exit

我把xargs -i kill -kill {}而不是xargs kill -9. 工作正常。这是一个好习惯吗?

scripts bash
  • 1 1 个回答
  • 204 Views

1 个回答

  • Voted
  1. Best Answer
    terdon
    2018-12-08T03:00:31+08:002018-12-08T03:00:31+08:00

    语法var=$(command)将运行command并将其输出分配给变量$var。这意味着脚本中的这一行:

    killProcess=$(sudo netstat -nlpt | grep 2020 | awk '{print $7}' |
                  sed 's/.\{5\}$//' | sort -u |  xargs -t kill -9)
    

    将始终运行以命令结尾的管道kill,并将其输出分配给$killprocess. 由于该kill命令没有输出,因此后面的这一行将永远不会做任何事情:

    $killProcess
    

    你想做的是killProcess="sudo netstat ... | xargs -t kill -9。但是您的命令也存在其他问题。首先,它将匹配任何包含 2020 的行,但您只需要2020端口所在的情况。如果它是 PID 怎么办?

    这是您的功能的工作版本,有一些更改:

    checkPort(){
      ## Get the target port. If none is given, default to 2020
      targetPort=${1:-2020}
    
      ## Collect the PID(s) that are listening on the port in the
      # array 'pids'.
      pids=( $(sudo netstat -nlpt | awk "\$4~/:$targetPort\$/{print \$7}" |
               sed 's|/.*||'))
      ## If no processes were listening, move on
      if [[ -z ${pids[0]} ]];
      then
        echo "No process listening on port $targetPort"
      ## If any processes were listening, kill them.
      else
        echo "killing process(es) listening on port $targetPort"
        ## First, try to kill the process gracefully
        kill "${pids[@]}"
        ## Check if any are still running
        for pid in "${pids[@]}"; do
          ## If it is still running
          if kill -0 "$pid"; then
            ## Kill it with fire
            kill -9 "$pid"
          fi
        done
      fi
    }
    

    这现在可以带一个参数,因此运行它checkport来检查端口 2020,checkport 22并将检查端口 22。它还首先尝试优雅地终止(kill -9除非绝对必要,否则您应该始终避免)并且仅kill -9在失败时使用。

    • 2

相关问题

  • 如何每 5 秒运行一次脚本?

  • 如何将必须从其自己的目录中运行的程序添加到面板或主菜单?

  • 如何编写 shell 脚本来安装应用程序列表?

  • Mac OS X Automator 的替代品?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve