@@ -24,6 +24,7 @@ import {
2424 LEGACY_GATEWAY_SYSTEMD_SERVICE_NAMES ,
2525 resolveGatewayServiceDescription ,
2626 resolveGatewaySystemdServiceName ,
27+ resolveLegacyGatewaySystemdServiceName ,
2728} from "./constants.js" ;
2829import { execFileUtf8 } from "./exec-file.js" ;
2930import { formatLine , toPosixPath , writeFormattedLines } from "./output.js" ;
@@ -93,8 +94,11 @@ const SYSTEM_SYSTEMD_UNIT_DIRS = [
9394 "/lib/systemd/system" ,
9495] as const ;
9596
96- async function findSystemSystemdUnitPath ( env : GatewayServiceEnv ) : Promise < string | null > {
97- const serviceFile = `${ resolveSystemdServiceName ( env ) } .service` ;
97+ async function findSystemSystemdUnitPath (
98+ env : GatewayServiceEnv ,
99+ name = resolveSystemdServiceName ( env ) ,
100+ ) : Promise < string | null > {
101+ const serviceFile = `${ name } .service` ;
98102 for ( const dir of SYSTEM_SYSTEMD_UNIT_DIRS ) {
99103 const candidate = path . posix . join ( dir , serviceFile ) ;
100104 try {
@@ -144,6 +148,30 @@ async function findMarkerOwnedSystemSystemdUnit(): Promise<{
144148 return null ;
145149}
146150
151+ /**
152+ * True when a scanned system unit name belongs to the gateway of THIS profile.
153+ *
154+ * The system-wide marker scan (`findSystemGatewayServices`) collects every
155+ * `openclaw-*` unit on the host; on multi-agent hosts that includes unrelated
156+ * running agents. A unit must match the canonical (`openclaw-gateway[-profile]`)
157+ * or legacy (`openclaw-<profile>`) name for the target profile before it may be
158+ * treated as the target's installation — otherwise doctor could report another
159+ * agent's PID as this gateway's runtime and recommend restarting the wrong
160+ * service (#119648).
161+ */
162+ function isSystemdGatewayUnitNameForProfile (
163+ unitName : string ,
164+ canonicalName : string ,
165+ profile : string | undefined ,
166+ ) : boolean {
167+ const normalized = unitName . replace ( / \. s e r v i c e $ / i, "" ) ;
168+ if ( normalized === canonicalName ) {
169+ return true ;
170+ }
171+ const legacyName = resolveLegacyGatewaySystemdServiceName ( profile ) ;
172+ return legacyName !== null && normalized === legacyName ;
173+ }
174+
147175/**
148176 * The full installed-gateway picture across both systemd scopes.
149177 *
@@ -193,10 +221,36 @@ async function findSystemSystemdGatewayScope(
193221 if ( systemPath ) {
194222 return { scope : "system" , unitName : canonicalUnitName , unitPath : systemPath } ;
195223 }
224+ // Older installs predate the `openclaw-gateway-` rename and use
225+ // `openclaw-<profile>`; resolve the same profile under that convention
226+ // before scanning system-wide (#119648).
227+ const hasUnitOverride = Boolean ( env . OPENCLAW_SYSTEMD_UNIT ?. trim ( ) ) ;
228+ const legacyName = hasUnitOverride
229+ ? null
230+ : resolveLegacyGatewaySystemdServiceName ( env . OPENCLAW_PROFILE ) ;
231+ if ( legacyName ) {
232+ const legacyPath = await findSystemSystemdUnitPath ( env , legacyName ) ;
233+ if ( legacyPath ) {
234+ return { scope : "system" , unitName : `${ legacyName } .service` , unitPath : legacyPath } ;
235+ }
236+ }
196237 // System-scope installs may use a non-canonical unit name; fall back to a
197- // marker-owned lookup before declaring no system unit exists.
238+ // marker-owned lookup, but only accept a unit that plausibly belongs to THIS
239+ // profile. An unrelated running `openclaw-*` unit (another agent on the same
240+ // host) must never be reported as this gateway's runtime, and doctor must
241+ // never recommend restarting it (#119648).
198242 const owned = await findMarkerOwnedSystemSystemdUnit ( ) ;
199- return owned ? { scope : "system" , unitName : owned . unitName , unitPath : owned . unitPath } : null ;
243+ if (
244+ owned &&
245+ isSystemdGatewayUnitNameForProfile (
246+ owned . unitName ,
247+ resolveSystemdServiceName ( env ) ,
248+ env . OPENCLAW_PROFILE ,
249+ )
250+ ) {
251+ return { scope : "system" , unitName : owned . unitName , unitPath : owned . unitPath } ;
252+ }
253+ return null ;
200254}
201255
202256/**
0 commit comments