Search code examples
bashprocessspinnerpid

How would i get a process pid which is running in a bash script and know when its done


I need to know an another process pid which is executed as a command in the bash script like a nmap scan etc as an when the process is created I can display a bar or a spinner #!/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

edit code is updated Thts the code in case tht was not visible properly ^^ As the pid dies when its completed I can use it as a count or a flag or something similar to end the spinner. I'm working on Kali nethunter.

P.S This is my first time on stack overflow pls forgive me if its too stupid or tell me if I missed something.


Solution

  • A starting point:

    #!/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