-1

Currently I am reading in host names from a file using a foreach command and using this hostname to connect to the device. All this is done within my TCL script. Is there a way I can run multiple instances of the script so that each device is queried separately? Something like a bash script similar to:

for hostname in file;
do
  log.tcl $hostname &
done

I believe this is similar to multi threading. Another question I have is that, when running multiple instances of a script, and each script writes to the same file, will all the logs be jumbled up?

user2883071
  • 960
  • 1
  • 20
  • 50

1 Answers1

1

There are several options to archive this:

1. Execute the script for each hostname:

foreach hostname $hosts {
    exec log.tcl $hostname &
}

This is like the bash solution.

2. Use threads

package require Thread
set pool [tpool::create]
set jobs {}
foreach hostname $hosts {
    lappend jobs [tpool::post -nowait $pool [list apply {{host} {
        set argv0 log.tcl
        set argv [list $host]
        source $argv0
    } $hostname]]
}
while {[llength $jobs]} {
     tpool::wait $pool $jobs jobs
}

Note that does not work nicely with threads.

For the other question regarding writing to the same file from multiple scripts: It depends. If you have a POSIX compliant system and open the files with a, then it might work.

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
  • 1
    Logging to files opened with `a` on a POSIX system will work reliably (assuming that the filesystem implements `O_APPEND` semantics correctly, which it _should_ do as it has enough information even in the networked filesystem case). – Donal Fellows Mar 10 '15 at 08:57
  • Thanks for the heads up with expect and threads. I guess I will be using the first option. When you mention opening files with `a`, do you mean, for example, opening a logfile as such: `log_file -a $filename`. Also, this may be because of my lack of understanding of POSIX systems, but say for example, a log is created at the beginning and end of the script, and instance for host_1 starts before instance for host_2. Then would the beginning log of host_2 be between the beginning and ending logs of host_1? – user2883071 Mar 10 '15 at 12:21