运行外部命令并并行处理其日志文件
我需要从 Perl 代码中运行外部工具。该命令运行相当长的时间,几乎不向 STDOUT 打印任何内容,但会创建一个日志文件。 我想运行它并并行读取和处理其日志文件。我怎样才能用 Perl 做到这一点?
提前致谢。
I need to run external tool from within my Perl code. This command works for a pretty long time, prints almost nothing to STDOUT but creates a log file.
I would like to run it and in parallel read and process its log file. How can I do it in Perl?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用类似 File::Tail 的内容来读取日志文件,那么您可以这样做一个简单的
fork
和exec
来运行外部命令。类似下面的内容应该可以工作:如果运行子进程出现问题,退出返回代码将位于特殊变量
$?
中;有关详细信息,请参阅wait
的文档。另外,如果日志输出没有提供何时停止跟踪文件的线索,您可以在 $SIG{CHLD} 中安装一个处理程序,它将捕获子进程的终止信号并允许您中断脱离循环。
If you use something like File::Tail to read the log file, then you can do a simple
fork
andexec
to run the external command. Something like the following should work:If there are problems running the child process, the exit return code will be in the special variable
$?
; see the documentation forwait
for more information.Also, if the logging output does not provide a clue for when to stop tailing the file, you can install a handler in
$SIG{CHLD}
which will catch the child process's termination signal and allow you to break out of the loop.