cqutwangyu commented on issue #15605:
URL:
https://github.com/apache/dolphinscheduler/issues/15605#issuecomment-1958847755
以下是我的测试代码 Demo.java
Here's my test code demo.java
```java
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
public class Demo {
public static void main(String[] args) throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(new File("/opt/test"));
processBuilder.redirectErrorStream(true);
processBuilder.command(args);
Process p = processBuilder.start();
int processId = getProcessId(p);
System.out.println("pid:"+processId);
System.out.println("isAlive:"+p.isAlive());
}
private static int getProcessId(Process process) {
int processId = 0;
try {
Field f = process.getClass().getDeclaredField("pid");
f.setAccessible(true);
processId = f.getInt(process);
} catch (Exception e) {
System.out.println("Get task pid failed"+e.getMessage());
}
return processId;
}
}
```
先编译java文件,并创建一个sh脚本文件
Compile the Java file first and create an sh script file
```
javac Demo.java
echo "java -jar server-0.0.1-SNAPSHOT.jar" > server-start.sh
```
执行sh脚本启动java程序,则获取到的pid是错误的。
Executing the sh script starts the Java program, and the pid is wrong.
```
java Demo sh server-start.sh
pid:35054
isAlive:true
jps
35080 Jps
35055 jar
```
直接执行java命令启动java程序,则能获取到正确的pid。
The direct execution of the Java command starts the Java program and gets
the correct pid.
```
kill -9 35055
java Demo java -jar server-0.0.1-SNAPSHOT.jar
pid:35641
isAlive:true
jps
35669 Jps
35641 jar
```
以下是运行环境
The following is the operating environment
```
java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
uname -a
Linux urc-esb-server2 3.10.0-514.el7.x86_64 #1 SMP Wed Oct 19 11:24:13 EDT
2016 x86_64 x86_64 x86_64 GNU/Linux
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]