gpt4 book ai didi

ant - 以不同的用户身份运行 ant

转载 作者:行者123 更新时间:2023-12-04 03:02:32 26 4
gpt4 key购买 nike

如果我以 root 身份运行任务,有没有办法检测它是否以 root 身份运行并以不同的用户身份运行某些任务。

我有一些任务需要以 root 身份运行,但其他任务只需要以当前用户身份运行。

最佳答案

如果当前用户具有特定名称(例如“root”),您可以使用类似以下的内容来仅运行特定目标。

<condition property="rootTasksEnabled">
<equals arg1="${user.name}" arg2="root" />
</condition>

<target name="do-stuff-if-root" if="rootTasksEnabled">
<echo>Doing root stuff</echo>
</target>

至于以不同的用户身份运行 Ant,您可以使用 和 su 命令来生成另一个 Ant 进程:

<target name="do-stuff" depends="do-stuff-if-root, do-other-stuff" />

<condition property="rootTasksEnabled">
<equals arg1="${user.name}" arg2="root" />
</condition>
<property name="targetToRunAsOtherUser" value="do-stuff-as-other-user" />
<property name="otherUser" value="johnny" />

<target name="do-stuff-if-root" if="rootTasksEnabled">
<echo>Doing root stuff</echo>

<exec executable="su">
<arg value="-c" />
<arg value="${ant.home}/bin/ant -buildfile ${ant.file} ${targetToRunAsOtherUser}" />
<arg value="${otherUser}" />
</exec>
</target>

<target name="do-other-stuff">
<echo>Doing normal build stuff</echo>
</target>

<target name="do-stuff-as-other-user">
<echo>I am running as ${user.name}</echo>
<echo>My home is ${user.home}</echo>
</target>

此示例仅适用于 Unix。要在 Windows 中执行此操作,您可能会使用 runas 命令而不是 su

关于ant - 以不同的用户身份运行 ant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5492378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com