Obviously, these examples are simplified.
Each first of the two, is the 'one-liner' and the 2nd code box shows the structure with idention.
1) In this first one, we're sending the output of the background job into a variable, which we echo at the end.
This works, but doesnt really take advantage of a 'background' job.
ret=$(( sleep 2 ; [ -d not-existing ] ; echo $?) &)
echo $ret
ret=$(
(
sleep 2
[ -d not-existing ]
echo $?
)&
)
echo $ret
2) This one applies alot more to what one usualy expects as a background job. (AFAIU) mkfifo lets the read-command wait until something is written to its passed file.
tmp=~/.cache/$$~ ; mkfifo "$tmp"
( sleep 10 ; [ -d not-existing ] ; echo $? > "$tmp") &
read RET < "$tmp" ; [ "$RET" = "0" ] && echo "GOOD!" || echo "BAD"
rm -f "$tmp"
tmp=~/.cache/$$~3) And to close the top 3, my most used code of these three.
mkfifo "$tmp"
(
sleep 10
[ -d not-existing ]
echo $? > "$tmp"
) &
read RET < "$tmp"
[ "$RET" = "0" ] && echo "GOOD!" || echo "BAD"
rm -f "$tmp"
This one loops while the background job is active and prints dots while its running. One could replace the printed string to update the text accordingly.
tmp="~/.cache/$~"
( sleep 5 ; [ -d not-existing ] ; echo $? > "$tmp") &
pid=$!
while ps $pid 1>/dev/zero;do sleep 0.5 ; printf ".";done
cat "$tmp"
tmp="~/.cache/$~"
(
sleep 5
[ -d not-existing ]
echo $? > "$tmp"
) &
pid=$!
while ps $pid 1>/dev/zero
do
sleep 0.5
printf "."
done
cat "$tmp"
Last but not least, i'll have a tool to help with such a task.
Its called: tui-psm, which names Text User Interface - Paralell Script Manager.
You can pass as many scripts as an array can hold, and limit the paralell executed scripts to any number you want (5 is default).
tui-psm script1.sh script2.bash script3.csh
But it can more, for example, if you need at least 3 scripts to be executed successfully before you attempt to run script 5, this could be your approach:
tui-psm -cq script1.sh script2.bash script3.csh ./script4.zsh
[ $? -ge 3 ] && ./script5.ash
Hope this helped.
Have fun scripting!
Keine Kommentare:
Kommentar veröffentlichen