【问题标题】:How would i get a process pid which is running in a bash script and know when its done我如何获得在 bash 脚本中运行的进程 pid 并知道它何时完成
【发布时间】:2019-02-06 20:26:48
【问题描述】:

我需要知道另一个进程 pid,它在 bash 脚本中作为命令执行,例如 nmap 扫描等,因为当创建进程时,我可以显示条形图或微调器 #!/bin/sh G='\033[0;32m' B='\033[0m' ${G}Enter IP TAIL LIKE 0.1 OR 1.1${B}" read v ${G}ENTER Device NAME ${B}" read k nmap -A -Pn -sV 192.168.$v -oN /tmp/op //here i want a while loop for displaying a spinner

编辑代码已更新Thts the code in case tht was not visible properly ^^ 由于 pid 在完成后死亡,我可以将其用作计数或标志或类似的东西来结束微调器。 我正在开发 Kali nethunter。

P.S 这是我第一次遇到堆栈溢出,如果太愚蠢,请原谅我,或者如果我遗漏了什么,请告诉我。

【问题讨论】:

  • 不要链接到您的代码的异地图像,而是将代码粘贴到此处,这样更容易阅读、批评和帮助。
  • 我添加了代码
  • ${sp:i++%${#sp}:1} 那里发生了什么?
  • 我还建议您确保到目前为止您编写的代码能够解决问题。我的系统上没有EchoReadNmap 命令,所以要么你在做一些时髦的事情,要么你没有投入最少的工作,你的意思是echoreadnmap

标签: bash process spinner pid


【解决方案1】:

起点:

#!/bin/bash

echo "Enter IP:"
read -r ip

# `&` runs in parallel
# always quote variables
nmap "$ip" &  
# `$!` get's the background process PID
pid=$!

...

# you can see if a pid is running by checking exit status of `kill -0`
while kill -0 "$pid" 2>&1 >/dev/null; do
    printf ...
    ...
done

【讨论】:

    【解决方案2】:
    nmap -A -Pn -sV 192.168.0.1 -p -oN  /tmp/op   >/dev/null & pid=$!
    i=1
    sp="/-\|"
    echo -n ' '
    while kill -0 "$pid" 2>&1 >/dev/null;
    do
    printf "\b${sp:i++%${#sp}:1}"
    done
    

    *感谢* kamil cuk

    解决了问题

    【讨论】:

      猜你喜欢
      • 2012-04-11
      • 2011-01-30
      • 2019-04-07
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      • 2012-08-25
      相关资源
      最近更新 更多