在tcl proc中在后台运行shell命令 [英] Running shell commands in background, in a tcl proc

查看:659
本文介绍了在tcl proc中在后台运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个tcl proc,将shell命令作为参数传递给它,然后打开一个临时文件,并将格式化的字符串写入该临时文件,然后在后台运行shell命令并将输出存储到临时文件也是如此.

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.

在后台运行命令,以便可以在之后立即调用proc 与另一个arg传递给它,写入另一个文件.因此,运行一百个这样的命令所花的时间不应该长于串行运行它们所花的时间.最终,可以将多个临时文件串联到一个文件中.

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?

任何帮助都将受到欢迎.我是tcl的新手,正在寻找解决办法.我读过有关在tcl中使用线程的信息,但是我正在使用不支持线程的tcl的较旧版本.

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 runthis { args }  { 
    set date_str [clock format [clock seconds] -format {+%Y%m%d-%H%M%S}]
    set tempFile ${date_str}.txt
    set output [ open $tempFile a+ ]
    puts $output "### Running $args ... ###"   
    close $output

    exec {*}$args >> $tempFile &
}

请参见 http://tcl.tk/man/tcl8.5/TclCmd/exec.htm

由于您的Tcl似乎比较旧,请替换

Since you seem to have an older Tcl, replace

    exec {*}$args >> $tempFile &

使用

    eval exec [linsert $args 0 exec] >> $tempFile &

这篇关于在tcl proc中在后台运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆