Jenkins插件开发,ProcStarter:如何写入已运行进程的stdin



我正在尝试写入已启动进程的stdin,该进程已在运行。在进程开始时写入sdtin有效:

外部脚本1.sh:

#!/bin/sh
echo "Started at $(date)" 
read a 
echo "$(date): got '$a'"

插件中的代码:

ProcStarter proc = launcher.launch().stderr(listener.getLogger()).stdout(listener.getLogger()).cmds("/tmp/1.sh");  
ByteArrayInputStream res = new ByteArrayInputStream(("hellooo" + System.lineSeparator()).getBytes(StandardCharsets.UTF_8));
proc.stdin(res).join();

jenkins控制台中的输出,启动时间和写入sdtin的时间戳相同:

$ /tmp/1.sh
Started at Thu Apr  2 10:52:02 CEST 2020
Thu Apr  2 10:52:02 CEST 2020: got 'hellooo'

我想不是在开始的时候,而是在x秒之后,写一个类似于以下伪代码的东西:

ProcStarter proc = launcher.launch().stderr(listener.getLogger()).stdout(listener.getLogger()).cmds("/tmp/1.sh");  
proc.join(); //blocks
//somewhere in another Thread:
Thread.sleep(15000);//pseudocode ;-)
ByteArrayInputStream res = new ByteArrayInputStream(("hellooo" + System.lineSeparator()).getBytes(StandardCharsets.UTF_8));
proc.stdin(res); // doesn't work

hudson的用法。Launcher.ProcStarter是必需的,因为它是在jenkins slave上执行的。有什么想法吗,我如何写到已经运行的进程的stdin?

我得到了解决方案,调用proc.writeStdin((方法很重要,该方法可以反转I/O方向:

ProcStarter procStarter = launcher.launch().stderr(listener.getLogger()).stdout(listener.getLogger()).cmds("/tmp/1.sh");  
proc.writeStdin();//important ==> Indicates that the caller will directly write to the child process stdin()
Proc myProc = procStarter.start();
myProc.getStdin().write(("helloooo2" + System.lineSeparator()).getBytes(StandardCharsets.UTF_8));
myProc.getStdin().flush();

最新更新