Notify background shutdown in bash

I execute several labor-intensive executables with different parameters assigned in a loop as background jobs in parallel. Here is an example of a toy and its result:

bash-3.2$ set -o notify
bash-3.2$ for (( i=0 ; i < 4 ; i+=2 )); do
>    sleep ${i} &
> done
[1] 32394
[2] 32395
bash-3.2$ [1]-  Done                    sleep ${i}
[2]+  Done                    sleep ${i}

Please note that when each task is completed, it will notify me. The job command will be shown in the final message, but so that the parameter is not expanded, so there is no obvious way to find out what parameter value it uses when viewing the message. Hope the post looks like

bash-3.2$ [1]-  Done                    sleep 0
[2]+  Done                    sleep 2

Can this be done?

Thank you and welcome!

+3
source share
2 answers

Not very pretty:

for (( i=0 ; i < 4 ; i+=2 )); do
    sleep ${i} && echo  -en "\nsleep ${i} "&
done

The result looks like this:

[1] 29311
[2] 29313

sleep 0 [1] - ${i} && & echo -en "\nsleep ${i}"
@: ~ $
sleep 2 [2] + ${i} && & echo -en "\nsleep ${i}"

:

myeval () {
    eval "$*" && echo -en "\n$* "
}

:

for (( i=0 ; i < 4 ; i+=2 )); do
    myeval "sleep ${i}"&
done

:

[1] 29635
[2] 29636

sleep 0 [1] - Done myeval "sleep ${i}"
@: ~ $
sleep 2 [2] + Done myeval "sleep ${i}"

+1

, .

for ((i=0; i < 4; i+=2 )); do
    long_running_process $i &
done

wait

long_running_process ( , script) $1 .

0

Source: https://habr.com/ru/post/1716719/


All Articles