2

I am looking to replicate the following bash shell script sequence but in a tclsh shell

for script in $listOfScripts
do
 ./$script &
done

wait
echo "Done"

I don't care about the exit status of each of those scripts (actually I want to suppress any eventual errors) and the order of completion does not matter. I can't seem to figure out how to wait for all the background processes to complete in order to continue with my code.

1 Answer 1

2

Assuming that you want to wait for them all to finish (and aren't too bothered about the order of that) the easiest thing to do is to open them as pipelines (those are implicitly backgrounded) and then to wait for all of them to finish.

foreach script $listOfScripts {
    # I like to be explicit when working with pipelines
    lappend pipes [open |$script r]
}
foreach pipe $pipes {
    close $pipe
}
puts "Done"

In 8.7, you also have ::tcl::process status, which means you can do this:

foreach script $listOfScripts {
    lappend pids [exec $script &]
}
set status [tcl::process status -wait $pids]
# Unlike with the shell, this is explicit
tcl::process purge $pids
puts "Done"
Sign up to request clarification or add additional context in comments.

2 Comments

Does that work with commands that produce a lot of output and fill up the pipe and block waiting for something to read it?
Thank you for this! But what do I do when one of the processes exits with an error? I want to ignore all the errors or exit codes and I don't care of the order they complete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.