I used Runtime.getRuntime().exec() to start external program in GREG handler, and get its pid. But how I can detect when the GREG stopping or restarting , that I can stop or restart the external program.
If you want to do execute some custom logic during startup or shutdown phases you can use handlers provided in org.wso2.carbon.core package
e.g.
ServerStartupHandler - https://github.com/wso2/carbon-kernel/blob/4.4.x/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerStartupHandler.java
ServerShutdownHandler - https://github.com/wso2/carbon-kernel/blob/4.4.x/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerShutdownHandler.java
ServerRestartHandler - https://github.com/wso2/carbon-kernel/blob/4.4.x/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerRestartHandler.java
Also you may use https://github.com/wso2/carbon-kernel/blob/4.4.x/core/org.wso2.carbon.core/src/main/java/org/wso2/carbon/core/ServerStartupObserver.java to listen to startup events