2017-01-13 5 views
0

私はアリでいくつかの基本的なターゲットを実行しました。私は今、さらに基本的なことをしようとしています。アリ:プロセスをリストする対象

私の目標は「デバッグ」のターゲットを用意して、psコマンドの特定の呼び出しの結果を得ることだけです。

$ 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 

私が動作していないよう基本的な目標を、しようとしている:私は手動で私のbashで呼び出した場合例えば

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

私はいくつかのバリエーションを試していますが、出力を削除し、argsなしで実行していますが、少し失われているようです。 私はスクリプトを作ってそれを実行し、結果をファイルにリダイレクトすることができますが、私は本当に標準でそれを持っていて、スクリプトを実行しないといけません。主に私はそれが可能でなければならないと思うから。

私はredhatで動作しています。私はosファミリでターゲットを保護する必要があることを知っています。

編集:私のターゲットの出力をポストするのを忘れ:

[[email protected] ]$ ./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) 
[[email protected]]$ 

私はエラーであるものを得ていることを知っていますが、私はパラメータなしで実行した場合、それは、何も、ない0や1を返しません何もない

edit2: 私は少しだけ私が得たいと思ったが、まだそこにはありません。

<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> 

答えて

0

ここではpsを実行して、「グレップ」に<linecontains>フィルタを使用して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> 

このスクリプトのみ起動ではなく2よりも、単一のプロセス(ps)(psgrep)、このスクリプトは高速になります。さらに、スクリプトは、クリーンアップする必要のある一時ファイルを生成しません。

+0

ありがとう –