SbloodyS commented on code in PR #17320:
URL: 
https://github.com/apache/dolphinscheduler/pull/17320#discussion_r2191360360


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/ProcessUtils.java:
##########
@@ -144,18 +155,54 @@ public static boolean kill(@NonNull TaskExecutionContext 
request) {
      */
     private static boolean sendKillSignal(String signal, String pids, String 
tenantCode) {
         try {
+            // 1. Send the kill signal
             String killCmd = String.format("kill -s %s %s", signal, pids);
             killCmd = OSUtils.getSudoCmd(tenantCode, killCmd);
             log.info("Sending {} to process group: {}, command: {}", signal, 
pids, killCmd);
             OSUtils.exeCmd(killCmd);
 
+            // 2. Wait for the process to respond to the signal
+            ThreadUtils.sleep(SLEEP_TIME_MILLIS * PROCESS_STATUS_CHECK_DELAY);
+
+            // 3. Check if the processes are still running
+            String[] pidArray = PID_PATTERN.split(pids);
+            for (String pid : pidArray) {
+                // Check if each PID is still alive
+                if (isProcessAlive(Integer.parseInt(pid), tenantCode)) {
+                    log.info("Kill command: {}, kill failed, the process: {} 
is still running", killCmd, pid);
+                    // Return false if any process is still alive
+                    return false;
+                }
+            }
+            log.debug("Kill command: {}, kill succeeded", killCmd);
+            // All processes have been successfully terminated
             return true;
         } catch (Exception e) {
             log.error("Error sending {} to process: {}", signal, pids, e);
             return false;
         }
     }
 
+    /**
+     * Check if a process with the specified PID is alive.
+     *
+     * @param pid the process ID to check
+     * @return true if the process exists and is running, false otherwise
+     */
+    private static boolean isProcessAlive(int pid, String tenantCode) {
+        try {
+            // Use kill -0 to check if the process exists; it does not 
actually send a signal
+            String checkCmd = String.format("kill -0 %d", pid);
+            checkCmd = OSUtils.getSudoCmd(tenantCode, checkCmd);
+            OSUtils.exeCmd(checkCmd);

Review Comment:
   I was wrong. It would throw `ExitCodeException`. Please ignore it.



-- 
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]

Reply via email to