hermes - 💡(How to fix) Fix gateway restart kills new instance on non-systemd environments (Termux/Android)

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

In _launch_detached_restart_command() (gateway/run.py), the Unix path uses:

subprocess.Popen([setsid_bin, "bash", "-lc", shell_cmd], start_new_session=True)

where shell_cmd = "while kill -0 $pid 2>/dev/null; sleep 0.2; done; hermes gateway restart".

bash -lc becomes the session leader. The watcher loop runs inside it. When the old gateway exits, bash finishes the -c command and exits — taking the newly launched gateway with it via SIGHUP. On systemd systems this is masked; on Termux/Android it fails deterministically.

Fix Action

Fix

Remove the intermediate bash subshell. Launch hermes gateway restart directly under setsid:

-        cmd = " ".join(shlex.quote(part) for part in hermes_cmd)
-        shell_cmd = (
-            f"while kill -0 {current_pid} 2>/dev/null; do sleep 0.2; done; "
-            f"{cmd} gateway restart"
-        )
+        if isinstance(hermes_cmd, list):
+            restart_parts = list(hermes_cmd) + ["gateway", "restart"]
+        else:
+            restart_parts = shlex.split(hermes_cmd) + ["gateway", "restart"]
         setsid_bin = shutil.which("setsid")
         if setsid_bin:
             subprocess.Popen(
-                [setsid_bin, "bash", "-lc", shell_cmd],
+                [setsid_bin, "--", *restart_parts],
                 stdout=subprocess.DEVNULL,
                 stderr=subprocess.DEVNULL,
                 start_new_session=True,
             )
         else:
             subprocess.Popen(
-                ["bash", "-lc", shell_cmd],
+                restart_parts,
                 stdout=subprocess.DEVNULL,
                 stderr=subprocess.DEVNULL,
                 start_new_session=True,
             )

Code Example

subprocess.Popen([setsid_bin, "bash", "-lc", shell_cmd], start_new_session=True)

---

-        cmd = " ".join(shlex.quote(part) for part in hermes_cmd)
-        shell_cmd = (
-            f"while kill -0 {current_pid} 2>/dev/null; do sleep 0.2; done; "
-            f"{cmd} gateway restart"
-        )
+        if isinstance(hermes_cmd, list):
+            restart_parts = list(hermes_cmd) + ["gateway", "restart"]
+        else:
+            restart_parts = shlex.split(hermes_cmd) + ["gateway", "restart"]
         setsid_bin = shutil.which("setsid")
         if setsid_bin:
             subprocess.Popen(
-                [setsid_bin, "bash", "-lc", shell_cmd],
+                [setsid_bin, "--", *restart_parts],
                 stdout=subprocess.DEVNULL,
                 stderr=subprocess.DEVNULL,
                 start_new_session=True,
             )
         else:
             subprocess.Popen(
-                ["bash", "-lc", shell_cmd],
+                restart_parts,
                 stdout=subprocess.DEVNULL,
                 stderr=subprocess.DEVNULL,
                 start_new_session=True,
             )
RAW_BUFFERClick to expand / collapse

Problem

When hermes gateway restart is triggered on Termux (no systemd), the new gateway instance dies immediately after launch, leaving the service completely down. QQ bot stays offline until someone manually restarts the gateway via TUI.

Root cause

In _launch_detached_restart_command() (gateway/run.py), the Unix path uses:

subprocess.Popen([setsid_bin, "bash", "-lc", shell_cmd], start_new_session=True)

where shell_cmd = "while kill -0 $pid 2>/dev/null; sleep 0.2; done; hermes gateway restart".

bash -lc becomes the session leader. The watcher loop runs inside it. When the old gateway exits, bash finishes the -c command and exits — taking the newly launched gateway with it via SIGHUP. On systemd systems this is masked; on Termux/Android it fails deterministically.

Fix

Remove the intermediate bash subshell. Launch hermes gateway restart directly under setsid:

-        cmd = " ".join(shlex.quote(part) for part in hermes_cmd)
-        shell_cmd = (
-            f"while kill -0 {current_pid} 2>/dev/null; do sleep 0.2; done; "
-            f"{cmd} gateway restart"
-        )
+        if isinstance(hermes_cmd, list):
+            restart_parts = list(hermes_cmd) + ["gateway", "restart"]
+        else:
+            restart_parts = shlex.split(hermes_cmd) + ["gateway", "restart"]
         setsid_bin = shutil.which("setsid")
         if setsid_bin:
             subprocess.Popen(
-                [setsid_bin, "bash", "-lc", shell_cmd],
+                [setsid_bin, "--", *restart_parts],
                 stdout=subprocess.DEVNULL,
                 stderr=subprocess.DEVNULL,
                 start_new_session=True,
             )
         else:
             subprocess.Popen(
-                ["bash", "-lc", shell_cmd],
+                restart_parts,
                 stdout=subprocess.DEVNULL,
                 stderr=subprocess.DEVNULL,
                 start_new_session=True,
             )

Testing

Restarted via QQ /restart on Termux — new gateway survives and connects within seconds. QQ stays online.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

hermes - 💡(How to fix) Fix gateway restart kills new instance on non-systemd environments (Termux/Android)