How can I use ant <exec> to execute commands on Linux?

I would like to use ant to invoke the command as shown below:

<exec executable="echo ptc@123 | sudo -S /app/Windchill_10.0/Apache/bin/apachectl -k stop"> </exec> 

But he is responsible for the error:

The characters around the executable and arguments are not part of the command.

Background: I want to use ant to stop the Apache server, but it is not installed by the same user. I am running a team.

Can anyone help or give me some tips?

Thank you in advance

+6
source share
1 answer

The Ant <exec> task uses the Java Process engine to run commands, and it does not understand shell-like syntax, such as pipes and redirects. If you need to use channels, you need to explicitly run the shell by saying something like

 <exec executable="/bin/sh"> <arg value="-c" /> <arg value="echo ptc@123 | sudo -S /app/Windchill_10.0/Apache/bin/apachectl -k stop" /> </exec> 

but in this case this is not necessary, since you can only run the sudo and use inputstring to provide your input, and not to use echo :

 <exec executable="sudo" inputstring=" ptc@123 &#10;"> <arg line="-S /app/Windchill_10.0/Apache/bin/apachectl -k stop" /> </exec> 

Since sudo -S requires a newline, I added &#10; at the end of an inputstring (this is the easiest way to encode a newline in an attribute value in XML).

Please note that <arg line="..." /> pretty simple when it comes to breaking words - if any of the command line arguments may contain spaces (for example, if you need to refer to a file in a directory, for example ${user.home}/Library/Application Support , or if the value is read from an external .properties file that you do not control), then you must separate the arguments yourself using separate arg elements with the value or file attributes, for example:

 <exec executable="sudo" inputstring=" ptc@123 &#10;"> <arg value="-S" /> <arg file="${windchill.install.path}/Apache/bin/apachectl" /> <arg value="-k" /> <arg value="stop" /> </exec> 
+17
source

All Articles