I'm running a shell test program that I can view a progress bar but when I run it I keep getting a unary error . Is kill -0 a way to kill a subprocess in shell ? Or is there another method to test if my process has died? heres my code to run a progress bar until my command ends:
#!/bin/sh
# test my progress bar
spin[0]="-"
spin[1]="\\"
spin[2]="|"
spin[3]="/"
sleep 10 2>/dev/null & # run as background process
pid=$! # grab process id
echo -n "[sleeping] ${spin[0]}"
while [ kill -0 $pid ] # wait for process to end
do
for i in "${spin[@]}"
do
echo -ne "\b$i"
sleep 0.1
done
done
enter code here
1. Is kill -0 a way to kill a subprocess in shell ?
On Linux OS,
kill -0is just a way to try to kill a process and see what happens, '0' is not a POSIX signal, it does nothing at all. If the process is running,killwill return0, if not, it will return1.ps $pid >/dev/null 2>&1could do the same job. To kill a process, one generally use theSIGQUIT/3(quit program) orSIGKILL/9(terminate program) ; the process could trap the signal and make a clean exit, or it could ignore the signal so the OS has to terminate it 'quick and dirty'.2. test and '['
/bin/[), and expect something you didn't provide correctly.whileiswhile list; do list; donewherelistwill return an exit code, so you don't have to use something else.3. how do I watch for a process to have died in shell script?
Like you did, the code below will do the job:
CAVEATS I use
/bin/bashas interpreter, as some of the Bourne Shell (sh) could not support the use of an array (iespin[n]).