gpt4 book ai didi

linux - 如何捕获从 shell 脚本抛出的错误并将其显示在 groovy 中?

转载 作者:太空宇宙 更新时间:2023-11-04 12:14:11 25 4
gpt4 key购买 nike

我基本上是在尝试与 Linux 机器建立 SSH 连接,并在我的 groovy 脚本中执行 shell 命令,如下所示:

#!groovy

def serverName51 = "********"
def folderName = "tmp"
node('linux') {
def output = sh returnStdout: true, script:"ssh -q karthik@${serverName51} 'cd /tmp; mkdir ${folderName};'"
}

上面的代码返回如下所示的错误消息:

ERROR: script returned exit code 255

我知道在执行 shell 脚本时会抛出一些错误。
如何在 groovy 中捕获和显示此错误?

最佳答案

所以你在这里向我们展示了一个 dsl 来执行 shell 脚本?

在您的 DSL 内部,您必须创建一个 Process 对象。我直接使用 java.lang.ProcessBuilder。

明确地:

Process process=new ProcessBuilder().directory(workingDir).command(["sh", "ls -l"]).start()

Process 有几种方法可以让您获得 shell 命令的输出以及退出代码:

/**
* Causes the current thread to wait, if necessary, until the
* subprocess represented by this {@code Process} object has
* terminated, or the specified waiting time elapses.
*
* <p>If the subprocess has already terminated then this method returns
* immediately with the value {@code true}. If the process has not
* terminated and the timeout value is less than, or equal to, zero, then
* this method returns immediately with the value {@code false}.
*
* <p>The default implementation of this methods polls the {@code exitValue}
* to check if the process has terminated. Concrete implementations of this
* class are strongly encouraged to override this method with a more
* efficient implementation.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the {@code timeout} argument
* @return {@code true} if the subprocess has exited and {@code false} if
* the waiting time elapsed before the subprocess has exited.
* @throws InterruptedException if the current thread is interrupted
* while waiting.
* @throws NullPointerException if unit is null
* @since 1.8
*/
public boolean waitFor(long timeout, TimeUnit unit)

/**
* Causes the current thread to wait, if necessary, until the
* process represented by this {@code Process} object has
* terminated. This method returns immediately if the subprocess
* has already terminated. If the subprocess has not yet
* terminated, the calling thread will be blocked until the
* subprocess exits.
*
* @return the exit value of the subprocess represented by this
* {@code Process} object. By convention, the value
* {@code 0} indicates normal termination.
* @throws InterruptedException if the current thread is
* {@linkplain Thread#interrupt() interrupted} by another
* thread while it is waiting, then the wait is ended and
* an {@link InterruptedException} is thrown.
*/
public abstract int waitFor() throws InterruptedException;

/**
* Returns the exit value for the subprocess.
*
* @return the exit value of the subprocess represented by this
* {@code Process} object. By convention, the value
* {@code 0} indicates normal termination.
* @throws IllegalThreadStateException if the subprocess represented
* by this {@code Process} object has not yet terminated
*/
public abstract int exitValue();

关于linux - 如何捕获从 shell 脚本抛出的错误并将其显示在 groovy 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47883831/

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