ant: 列出进程的目标

ant: target to list processes

我已经在 ant 中完成了一些基本目标,现在我正在尝试做一些更基本的事情。

我的目标是为 "debug" 设置一个目标,我可以在其中获取对 ps 命令的特定调用的结果。

例如,如果我手动调用 bash:

$ ps -ef | grep /bin/bash
502       1373     1  0 05:28 ?        00:00:00 /bin/bash MyScript.sh /data/dir  > MyScript.log
502      32314 31196  0 09:42 pts/0    00:00:00 grep /bin/bash

我正在尝试一个基本目标,但它似乎不起作用。

  <target name="list-processes" depends="init">
        <exec executable="ps" spawn="false" output="Yes">
           <arg line=" -ef | grep /bin/bash" />
        </exec>
  </target>

我尝试了几种变体,删除了输出,运行ning 没有 args 等等,但似乎有点 lost。 我可以有一个脚本和 运行 它,并且可能将结果重定向到一个文件,但我真的很想把它放在标准输出中而不为它做一个脚本。主要是我觉得应该是possible.

我运行正在使用 redhat,我知道我应该和 os 家族一起保护目标。

编辑:忘记 post 我的目标的输出:

[user@instance ]$ ./script.sh target.list-processes
09:47:28.605: EXECUTE COMMAND 'target.list-processes' [Target Rev. c057925fc5b586013fd4ea903a65115dd42d4ceb]
09:47:29.190: Result: 1
09:47:29.192: COMMAND 'target.list-processes' SUCCESSFUL (execution time: 0 seconds)
[user@instance]$

我知道我得到的是一个错误,但是如果我 运行 没有参数它 returns 什么都没有,不是 0 也不是 1,也不是什么

编辑2: 我对我想要得到的东西有点 closer,但仍然没有。

  <target name="list-processes" depends="init">
    <exec executable="ps" osfamily="unix" outputproperty="list-processes-output" resultproperty="list-processes-result">
      <arg line="-ef" />
    </exec>
    <echo message="exec ps result: ${list-processes-result}" />
    <exec executable="grep" osfamily="unix" outputproperty="grep-processes-output" resultproperty="grep-processes-result">
      <arg line="bash" />
    </exec>
    <echo message="${grep-processes-output}" />
    <echo message="exec grep result: ${grep-processes-result}" />
  </target>

列表或进程没问题,但我不知道如何在 grep 中使用它,因为我没有在文件中。

好的,我终于找到了解决方案,但老实说不是很高兴,但它确实有效。 如果有更好的解决方案,请告诉我

10:24:27.268: exec ps result: 0
10:24:27.676: 502       1373     1  0 05:28 ?        00:00:00 /bin/bash Script.sh /data/dir > Script.log
10:24:27.677: exec grep result: 0

这是最终目标

  <target name="list-processes" depends="init">
    <exec executable="ps" osfamily="unix" outputproperty="list-processes-output" resultproperty="list-processes-result">
      <arg line="-ef" />
    </exec>
    <echo message="exec ps result: ${list-processes-result}" />
    <echo message="${list-processes-output}" file="processes.txt"/>
    <exec executable="grep" osfamily="unix" outputproperty="grep-processes-output" resultproperty="grep-processes-result">
      <arg line="/bin/bash processes.txt" />
    </exec>
    <echo message="${grep-processes-output}" />
    <echo message="exec grep result: ${grep-processes-result}" />
  </target>

这是一个运行 ps 然后使用 <linecontains> 过滤器对 "grep" 输出的 Ant 脚本:

<project name="ant-exec-ps-grep" default="run">
    <target name="run">
        <exec executable="ps">
            <arg value="-ef"/>
            <redirector outputproperty="list-processes-output">
                <outputfilterchain>
                    <linecontains>
                        <contains value="/bin/bash"/>
                    </linecontains>
                </outputfilterchain>
            </redirector>
        </exec>
        <echo>list-processes-output: ${list-processes-output}</echo>
    </target>
</project>

此脚本仅启动一个进程 (ps) 而不是两个进程(psgrep),因此此脚本会更快。此外,该脚本不会生成任何需要清理的临时文件。