如何记录 Ant 脚本随时间生成的所有子进程?

How do I record all child processes spawned by an Ant script over time?

我继承了一个遗留的基于 Ant 的构建系统,我正试图了解它的范围。我用 fork=yes 观察到多个 jvmjunit 任务。它疯狂地调用 subant 和类似的任务。偶尔,它只是 exec 其他进程。

我真的不想为每个任务搜索 100 多个脚本和参考文档来查找可能的分叉行为。我想在构建 运行s.

时捕获子进程列表

我设法为构建创建了一个干净的 Vagrant + Puppet 环境,我可以 运行 像这样的完整构建

$ cd /vagrant && $ANT_HOME/bin/ant 

如果我不得不强制执行某项操作...我会让脚本启动构建并捕获子进程直到构建完成?

#!/bin/bash

$ANT_HOME/bin/ant &
while ps $!
do
  sleep 1
  ps --ppid $! >> build_processes
done

由于ant是一个java进程,你可以尝试使用byteman。在 byteman 脚本中,您可以定义在执行 java.lang.Runtime 中的方法 exec 时触发的规则。

您使用 ANT_OPTS env 变量将 byteman 附加到 ant。

网友Jayan推荐strace,具体来说:

$ strace -f -e trace=fork ant

-f 将跟踪限制为 fork 系统调用。

Trace child processes as they are created by cur- rently traced processes as a result of the fork(2) system call. The new process is attached to as soon as its pid is known (through the return value of fork(2) in the parent process). This means that such children may run uncontrolled for a while (espe- cially in the case of a vfork(2)), until the parent is scheduled again to complete its (v)fork(2) call. If the parent process decides to wait(2) for a child that is currently being traced, it is suspended until an appropriate child process either terminates or incurs a signal that would cause it to terminate (as determined from the child’s current signal dis- position).

我找不到 trace=fork 表达式,但 trace=process 似乎很有用。

-e trace=process

Trace all system calls which involve process management. This is useful for watching the fork, wait, and exec steps of a process.

http://linuxcommand.org/man_pages/strace1.html