I have a very simple script that I use to wait for a process to complete. The script is used preceding calls to tasks, commands that depend on the process completion. If the process is ready the desired command will run. The format is:
await_script_while_condition_not_met && cmd2
Which means that await_script_while_condition_not_met
will loop until the condition is met and only after the cmd2 runs.
Docker-compose appears to output messages to stderr. In Bash > /dev/null
redirects stdout and not stdeer. To redirect both to /dev/null
, I have to redirect stderr to stdout.
#!/usr/bin/env bash
is_npm_install_running () {
docker-compose run node test -d node_modules/.staging 2>&1 > /dev/null
}
main () {
while is_npm_install_running; do
sleep 30
done
}
main & PID=$!
echo ''
echo ''
echo 'This may take a while, please be patient!'
echo 'Npm install is still running...'
echo ''
while kill -0 $PID 2> /dev/null; do
printf "▓"
sleep 2
done
echo 'Done!'
The 2>&1 > /dev/null
does not prevent Docker-compose to output the initialization messages you can see in the image screenshot bellow Starting docker_XXXX...done!, etc
:
If the process is long there'll be a big number of repeated messages that are not necessary and I'd like to hide. The script I have is quite simple and it's only used to await
for the process to finish before allowing the user to proceed to run certain services, tasks or commands: a nice to have, but not absolutely necessary! A sort of prettifier.
This video demo demonstrates the script in action and working for the case > /dev/null
(have in mind the messages I want to hide are present and keep increasing for each iteration), the case for the source code above 2>&1 > /dev/null
works similarly.
Edit
This is marked as duplicate but the answers present in the suggested posts would not work as exposed in the current message; also as documented here.