0

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:

enter image description here

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
punkbit
  • 7,347
  • 10
  • 55
  • 89
  • Could you just use `-d` (detach) so that this runs in the background? – halfer May 20 '19 at 17:04
  • @John Kugelman, you've marked as duplicate but the answers in those topics as you can see from the tests I've shared, will not work. Please remove the flag as this will be useful for other users in the future! – punkbit May 20 '19 at 17:35
  • You need to write `> /dev/null 2>&1` instead of `2>&1 > /dev/null`. – John Kugelman May 20 '19 at 17:37
  • @JohnKugelman, that was tested before posting the topic and did not work, the answer is `2> /dev/null`. – punkbit May 20 '19 at 17:44

1 Answers1

3

The solution I found that works is 2> /dev/null

For the case shared above this should read:

docker-compose run node test -d node_modules/.staging 2> /dev/null
punkbit
  • 7,347
  • 10
  • 55
  • 89