[英]How can I use ant <exec> to execute commands on linux?
I would like to use ant to exectue a command like below: 我想使用ant来执行如下命令:
<exec executable="echo ptc@123 | sudo -S /app/Windchill_10.0/Apache/bin/apachectl -k stop">
</exec>
But it replies an error say 但是它回复了一个错误说
The ' characters around the executable and arguments are not part of the command. '可执行文件和参数周围的字符不是命令的一部分。
The background is: I want to use ant to stop the apache server but it doesn't installed by the same user I run the command. 背景是:我想使用ant来停止apache服务器但是我没有安装它运行命令的同一个用户。
Anyone could help or give me some clues? 有人可以帮忙或给我一些线索吗?
Thanks in advance 提前致谢
Ant's <exec>
task uses Java's Process
mechanism to run commands, and this does not understand shell-specific syntax like pipes and redirections. Ant的
<exec>
任务使用Java的Process
机制来运行命令,而这并不理解特定于shell的语法,如管道和重定向。 If you need to use pipes then you have to run a shell explicitly by saying something like 如果你需要使用管道,那么你必须通过说出类似的东西来明确地运行shell
<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 it's not necessary, as you can run just the sudo
command and use inputstring
to provide its input rather than using a piped echo
: 但在这种情况下没有必要,因为你可以只运行
sudo
命令并使用inputstring
来提供输入而不是使用管道echo
:
<exec executable="sudo" inputstring="ptc@123 ">
<arg line="-S /app/Windchill_10.0/Apache/bin/apachectl -k stop" />
</exec>
Since sudo -S
requires a newline character to terminate the password, I've added
由于
sudo -S
需要换行字符才能终止密码,所以我添加了
on the end of the inputstring
(this is the simplest way to encode a newline character in an attribute value in XML). 在
inputstring
的末尾(这是在XML中的属性值中编码换行符的最简单方法)。
Note that <arg line="..." />
is pretty simple minded when it comes to word splitting - if any of the command line arguments could contain spaces (for example if you need to refer to a file under a directory such as ${user.home}/Library/Application Support
or if the value is read from an external .properties
file that you don't control) then you must split the arguments up yourself using separate arg
elements with value
or file
attributes, eg: 请注意,
<arg line="..." />
在分词时非常简单 - 如果任何命令行参数可以包含空格(例如,如果您需要引用目录下的文件,例如${user.home}/Library/Application Support
或者如果从您不控制的外部.properties
文件中读取值,则必须使用具有value
或file
属性的单独arg
元素自行拆分参数,例如:
<exec executable="sudo" inputstring="ptc@123 ">
<arg value="-S" />
<arg file="${windchill.install.path}/Apache/bin/apachectl" />
<arg value="-k" />
<arg value="stop" />
</exec>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.