I'm trying to create a tcl proc, which is passed a shell command as argument and then opens a temporary file and writes a formatted string to the temporary file, followed by running the shell command in background and storing the output to the temp file as well.
Running the command in background, is so that the proc can be called immediately afterwards with another arg passed to it, writing to another file. So running a hundred such commands should not take as long as running them serially would do. The multiple temp files can finally be concatenated into a single file.
This is the pseudocode of what I'm trying to do.
proc runthis { args }
{
set date_str [ exec date {+%Y%m%d-%H%M%S} ]
set tempFile ${date_str}.txt
set output [ open $tempFile a+ ]
set command [concat exec $args]
puts $output "### Running $args ... ###"
<< Run the command in background and store output to tempFile >>
}
But how do I ensure the background'ing of the task is done properly? What would need to be done to ensure that the multiple temp files get closed properly?
Any help would be welcome. I'm new at tcl and finding to get my mind around this. I read about using threads in tcl but I'm working with an older version of tcl which doesn't support threading.
proc
itself (well, there can be quoted newlines in between, e.g. in the argument list or backslashed, but that's a refinement that most people don't bother with). - Donal Fellowsclock format
andclock seconds
to do the work ofexec date
. Mind you, to keep output from different runs separate you also need a sequence number (in case two runs start within the same second, of course). - Donal Fellowsfile tempname
since 8.6 ormkstemp(3)
-emulating code; as a personal experience, I find Stu's version to be just OK for older Tcls). - kostix