Am 15.11.2020 um 10:29 schrieb Tal Yanai <t...@yanai.org.il>:Hi,I'm trying to execute a simple /bin/csh command from within Java (I know I can use the Shell exec Step, but want to try and parse some data comes in from the stdout of the Linux command using Java.
I'm using ProcessBuilder for that, however, Jenkins always running this on the controller environment, and not on the Agent. (I have controller on Windows and Agent on Linux)Anything I can do to make sure Runtime.exec (or ProcessBuilder) is running on the Agent and not on the Controller?
Also, and proven way to parse the output coming from a Linux shell execution using Java?
Thanks,Tal.--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/073df562-c83b-4787-afcc-fcb377b3fd03n%40googlegroups.com.
There’s a process user must execute in shell, that produce some lines of code I want to parse. I’m trying to do the all thing from the Java plugin to spare the shell usage to begin with.
The call is made from hudson.tasks.Builder (perform), and is always running on the Controller, not on the agent.
--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/c7Z3OFLyzx0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/937E1434-52AA-489D-A610-E732CB7B882D%40gmail.com.
I see ProcLauncher using the Launcher. How can I use ProcessBuilder, or what is the difference?
From: jenkin...@googlegroups.com <jenkin...@googlegroups.com> On Behalf Of Jesse Glick
Sent: יום א 15 נובמבר 2020 16:57
To: Jenkins Dev <jenkin...@googlegroups.com>
Subject: Re: Running Java ProcessBuilder on Agent
Use the `Launcher` you are provided.
--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/c7Z3OFLyzx0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr3GKnFRoCNQRFeGZHtbExZuPy5ncYvhuX24LhtXqJ1f4Q%40mail.gmail.com.
Thanks Jesse,
I’m getting closer, but fail to understand how to get back the stdout of the process:
-----------------------------------------------------------------------------------
ProcStarter ps = launcher.new ProcStarter();
ps.cmds(command); //.stdout(listener);
Proc proc = launcher.launch(ps);
BufferedReader in = new BufferedReader(new InputStreamReader(ps.stdin(), StandardCharsets.UTF_8));
String s = null;
while ((s = in.readLine()) != null) {
this.jobListener.getLogger().print(s);
}
I see the process runs OK, but nothing is being returned back.
Thanks,
Tal.
From: jenkin...@googlegroups.com <jenkin...@googlegroups.com> On Behalf Of Jesse Glick
Sent: יום א 15 נובמבר 2020 16:57
To: Jenkins Dev <jenkin...@googlegroups.com>
Subject: Re: Running Java ProcessBuilder on Agent
Use the `Launcher` you are provided.
--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/c7Z3OFLyzx0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr3GKnFRoCNQRFeGZHtbExZuPy5ncYvhuX24LhtXqJ1f4Q%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/007a01d6bb6c%24babdeec0%243039cc40%24%40yanai.org.il.
Yes, I need to read the STDOUT, but using an InputStream. Doing proc.getStdout() throws a NullPointer and no other way that I can see that returns InputStream.
If I can’t get the InputStream, there’s something I don’t do right here.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CAG%3D_DusaX7LL%2Bi7S0CK48FRt4dBrZ0%3Dcz0-XeSHyET3KB7MewQ%40mail.gmail.com.
This is how I solved it eventually:
public void execBatchCommand(FilePath filePath) throws IOException, InterruptedException {
String[] command = {“/bin/csh”, “/home/tyanai/run.sh”, “input_param”};
filePath.act(new FileCallable<Void>() {
private static final long serialVersionUID = 6166111757469534436L;
@Override
public void checkRoles(final RoleChecker checker) throws SecurityException {
}
@Override
public Void invoke(final File workspace, final VirtualChannel channel) throws IOException {
String sessionNameToMonitor = null;
boolean foundGoodVSIF = false;
try {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
Process proc = builder.start();
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String s = null;
while ((s = in.readLine()) != null) {
jobListener.getLogger().print(s + "\n");
}
} catch (IOException e) {
jobListener.getLogger().println(e.getMessage());
for (StackTraceElement ste : e.getStackTrace()) {
jobListener.getLogger().println(" " + ste);
}
jobListener.getLogger().println(ExceptionUtils.getFullStackTrace(e));
}
return null;
}
});
}
From: ga...@gavinmogan.com <ga...@gavinmogan.com> On Behalf Of 'Gavin Mogan' via Jenkins Developers
Sent: יום א 15 נובמבר 2020 18:36
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CAG%3D_DusaX7LL%2Bi7S0CK48FRt4dBrZ0%3Dcz0-XeSHyET3KB7MewQ%40mail.gmail.com.