views:

308

answers:

2

Hi, I am running some time-consuming executables with different parameters assigned in a loop as background jobs in parallel. Here is a toy example and its output:

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}

Notice that when each job finishes running, it will notify me. In the finishing message the command of the job will be shown, but in the way that the parameter is not expanded, so there is not obvious way to tell what parameter value it is using by looking at the message. What I hope the message is like

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

Is it possible to do so?

Thanks and regards!

+1  A: 

Not very pretty:

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

Output looks like:

[1] 29311
[2] 29313

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

A function could neaten it up a bit:

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

Then you can do:

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

and the output looks like:

[1] 29635
[2] 29636

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

Dennis Williamson
A: 

If you're wanting to wait till the last one is done..

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

wait

Each long_running_process (assuming its a shell script) can print out a status message and its own $1 immediately before termination.

Shin
But then he doesn't get the immediate feedback that `set -o notify` gives him.
Dennis Williamson