Просмотр исходного кода

fix: handle case that zombie process terminated

When emqx is stopped, sometimes it found out that the process is at
zombie state and tries to get the parent PID but the process has already
finished in the meanwhile, printing an error message like this:

```
error: process ID list syntax error

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
WARNING: 281777 is marked <defunct>, parent:
ok
```
Paulo Zulato 2 лет назад
Родитель
Сommit
1b533794aa
1 измененных файлов с 11 добавлено и 1 удалено
  1. 11 1
      bin/emqx

+ 11 - 1
bin/emqx

@@ -805,6 +805,12 @@ generate_config() {
     mv -f "$TMP_ARG_FILE" "$ARGS_FILE"
 }
 
+# check if a PID is defunct
+is_defunct() {
+    local PID="$1"
+    ps -fp "$PID" | $GREP -q 'defunct'
+}
+
 # check if a PID is down
 # shellcheck disable=SC2317 # call in func `nodetool_shutdown()`
 is_down() {
@@ -812,9 +818,13 @@ is_down() {
     if ps -p "$PID" >/dev/null; then
         # still around
         # shellcheck disable=SC2009 # this grep pattern is not a part of the program names
-        if ps -fp "$PID" | $GREP -q 'defunct'; then
+        if is_defunct "$PID"; then
             # zombie state, print parent pid
             parent="$(ps -o ppid= -p "$PID" | tr -d ' ')"
+            if [ -z "$parent" ] && ! is_defunct "$PID"; then
+                # process terminated in the meanwhile
+                return 0;
+            fi
             logwarn "$PID is marked <defunct>, parent: $(ps -p "$parent")"
             return 0
         fi