'Tcl shell: execute multiple background scripts and wait for completion
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.
Solution 1:[1]
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"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Donal Fellows |
