From 852a3771e63eaf1083e165ead723e63683034e1b Mon Sep 17 00:00:00 2001 From: vkpdeveloper Date: Thu, 26 Nov 2026 02:21:31 +0530 Subject: [PATCH] fix(cli): Linux service points at its journal for logs On Linux, systemd keeps the service's output in the journal, but `vmbridge service install` pointed at ~/.vmbridge/vmbridge.log, which never exists there. For the same reason, `vmbridge` in a terminal listed no addresses for the running service. Both now read the journal on Linux. --- apps/cli/src/index.ts | 10 +++++----- apps/cli/src/service.ts | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/apps/cli/src/index.ts b/apps/cli/src/index.ts index 8f7903f..765fab8 100644 --- a/apps/cli/src/index.ts +++ b/apps/cli/src/index.ts @@ -1,10 +1,10 @@ #!/usr/bin/env bun -import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { parseArgs } from "node:util"; import { compiled, dashboardKey, dataDir, defaultPort, start, version } from "@vmbridge/server"; import { pidFile, restartSignal, runningPid, stopProcess } from "./process.ts"; -import { logPath, protectLog, service, serviceAddresses } from "./service.ts"; +import { protectLog, service, serviceAddresses } from "./service.ts"; import { update, updateFromDashboard } from "./update.ts"; const help = `VM Bridge ${version} @@ -168,9 +168,9 @@ async function run(options: { host?: string; port: number }) { process.on(restartSignal, () => stop(true)); } -function backgroundNotice(pid: number) { +async function backgroundNotice(pid: number) { console.log(`VM Bridge is already running in the background (process ${pid}), and keeps running after this terminal closes.\n`); - const addresses = existsSync(logPath()) ? serviceAddresses(readFileSync(logPath(), "utf8")) : []; + const addresses = serviceAddresses(await service.output()); for (const address of addresses) console.log(address); if (addresses.length) console.log(""); console.log("To run it in this terminal instead, stop the background service first with `vmbridge stop`."); @@ -216,7 +216,7 @@ async function main() { if (existing) throw new Error(`VM Bridge is already running in a terminal (process ${existing}). Stop it first.`); const options = serverOptions(values); await service.install(options); - return console.log(`VM Bridge now runs in the background on port ${options.port}. Logs: ${logPath()}`); + return console.log(`VM Bridge now runs in the background on port ${options.port}. Logs: ${service.logLocation()}`); } if (subcommand === "uninstall") { await service.uninstall(); diff --git a/apps/cli/src/service.ts b/apps/cli/src/service.ts index 2c29a7a..23aa028 100644 --- a/apps/cli/src/service.ts +++ b/apps/cli/src/service.ts @@ -1,12 +1,12 @@ import { spawn } from "node:child_process"; -import { chmodSync, closeSync, existsSync, mkdirSync, openSync, rmSync, writeFileSync } from "node:fs"; +import { chmodSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { childEnv, compiled, dataDir } from "@vmbridge/server"; const label = "dev.vmbridge"; -/** The background service's output on macOS. It can hold provider errors and addresses, so only the owner reads it. */ +/** The background service's output on macOS (Linux uses the journal). It can hold provider errors and addresses, so only the owner reads it. */ export const logPath = () => join(dataDir(), "vmbridge.log"); /** launchd appends to the log with whatever mode it already has, and makes a new one world-readable. */ @@ -151,6 +151,13 @@ ${env} await this.stop(); rmSync(this.plist(), { force: true }); }, + + /** Where to read the service's output. */ + logLocation: () => logPath(), + + async output() { + return existsSync(logPath()) ? readFileSync(logPath(), "utf8") : ""; + }, }; const systemdQuote = (value: string) => `"${value.replaceAll("\\", "\\\\").replaceAll('"', '\\"').replaceAll("%", "%%")}"`; @@ -224,6 +231,13 @@ WantedBy=default.target rmSync(this.unit(), { force: true }); await exec(["systemctl", "--user", "daemon-reload"]); }, + + /** systemd keeps the service's output in the journal, not in a file. */ + logLocation: () => "journalctl --user -u vmbridge", + + async output() { + return (await exec(["journalctl", "--user", "--unit", "vmbridge.service", "--output", "cat", "--lines", "200", "--no-pager"])).stdout; + }, }; const unsupported = { @@ -235,6 +249,8 @@ const unsupported = { start: async () => {}, detached: async (_: string[], __: string) => 1, uninstall: async () => {}, + logLocation: () => "", + output: async () => "", }; export const service = process.platform === "darwin" ? launchd : process.platform === "linux" ? systemd : unsupported;