Reputation: 99960
I would like to run a program conditionally in the background, if the condition is met, we run it as a daemon, otherwise we wait for the result:
if [ -z "${CONDITION}" ]; then
echo "background" && npm install -g yarn &
else
echo "foreground" && npm install -g yarn
fi
is there a shorthand way to do this? (I assume in the else block, that given the syntax that this process will not run in the background). I guess we could also conditionally add "wait" instead of an "&" character.
Upvotes: 8
Views: 2044
Reputation: 1545
I think your wait
idea is good. I can't think of a better way.
npm install -g yarn &
[ -n "$CONDITION" ] && { echo "waiting for install"; wait; }
Not sure that you want the echo
stuff or not, but if it's time consuming, you might want an indication.
HTH
Update: Simple script to show how it works
C=""
date
sleep 5s &
[ -n "$C" ] && { echo "waiting"; wait; }
date
If C=""
, you get an output like so:
Thu Dec 8 11:42:54 CET 2016
Thu Dec 8 11:42:54 CET 2016
(and of course, sleep 5s
is still running while your script is finished)
If you set C="something"
, you get:
Thu Dec 8 11:42:42 CET 2016
waiting
Thu Dec 8 11:42:47 CET 2016
Upvotes: 8