Jeegar Patel Jeegar Patel - 5 months ago 17
Bash Question

Run while loop until background process finish its job

In linux shell script i have

vlc /some/file/path.mkv &
wait


Now until my background process return
wait
call will be blocked. But here i want that until my background process return i want to execute one loop and in that print some data continuous. So when ever my background process return/exit i need to break that loop.

How to do that in shell script?

Answer

See if this works for you

vlc /some/file/path.mkv &
while [[ -n $(jobs -r) ]]; do echo -n "some data"; sleep 1; done

jobs -r checks running processes, and printing something, the script stops when your vlc process is done.