Run it

Running it unattended

Backgrounding and durability are two different problems. On this machine both <code>nohup</code> and <code>setsid</code> survived the parent shell exiting - the processes simply got reparented - and both died together on <code>wsl --shutdown</code>. So the usual advice of swapping one for the other fixes nothing. What survives a restart is a unit file with a restart policy, and what decides whether the port is safe is the bind address, not the process manager.

The advice you find for this problem is almost always about the wrong thing. It says nohup will not save you and you need setsid, or the reverse. We ran both and watched, and the answer on this machine was that neither made a difference - because the failure nobody demonstrates is not the shell exiting. It is the machine going away.

Backgrounding and durability are different problems

Closing the terminal raises one question: does the process outlive its parent. Rebooting the machine raises another: does anything bring it back. The commands people argue about only affect the first question, and only sometimes.

The experiment is short. Start two sleeps, one under nohup and one under setsid, then let the parent shell exit and look again from a new session:

$ wsl.exe -d Ubuntu -- bash -lc 'nohup sleep 150 & setsid sleep 150 &'
$ # parent shell exits here
$ wsl.exe -d Ubuntu -- bash -lc 'ps -eo pid,ppid,args | grep "sleep 150"'
    631     618 sleep 150
    632     618 sleep 150

Both alive. And note the parent id: 618, not 1. The processes were reparented to something still running inside the distribution, so neither nohup nor setsid was doing the work that the folklore credits them with. On this machine the difference between them was unobservable.

The part that actually stops things

The same two processes, a moment later:

$ wsl.exe --shutdown
$ wsl.exe -d Ubuntu -- bash -lc 'ps -eo pid,ppid,args | grep "sleep 150"'
全部消失(含 PPID=618 那两个)

Everything gone, including the process that had adopted them. A Windows reboot, a host restart, or a shutdown of the distribution takes the whole tree down, and the way a process was started has no bearing on that. This is the honest version of “my agent stopped overnight”: it was not killed by the terminal, it was killed by the machine.

What the machine already has

The mechanism that survives restarts is the init system, and it is present:

$ ps -p 1 -o comm=
systemd
$ systemctl is-system-running
running
$ systemctl list-unit-files | grep -i hermes
无 hermes unit

So the tooling is there and nothing is using it. A unit with a restart policy is the difference between a process you started and a service that is expected to be up. Three properties matter more than the exact content:

  • Restart on failure, with a delay, so a crash loop does not look like a healthy uptime graph.
  • A working directory and environment, because a service does not inherit your shell, your activated virtualenv, or the proxy variables you exported by hand.
  • Logs somewhere findable, since the first question after an unattended failure is always “what did it print” and the answer cannot be a terminal that closed.

That last point is the one people skip. Under a service manager the output goes to the journal, not to a terminal buffer, and a service that failed at 03:00 with no readable log is indistinguishable from a service that never started.

What the unit actually needs to contain

A skeleton is enough to see where the usual attempt goes wrong. Ours is not installed on this machine - the placeholders below are placeholders, and that is the point: the file has to name an absolute interpreter path and an environment, because nothing from your shell is inherited.

[Unit]
Description=Agent backend
After=network-online.target

[Service]
Type=simple
WorkingDirectory=/home/<user>/.hermes/hermes-agent
Environment="PATH=/home/<user>/.hermes/hermes-agent/venv/bin:/usr/local/bin:/usr/bin:/bin"
ExecStart=/home/<user>/.hermes/hermes-agent/venv/bin/hermes serve --host 127.0.0.1 --port 9119
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=default.target

Two lines carry most of the value. Environment is what makes a virtualenv-based tool start at all under a manager that has no idea what you activated by hand. Restart=on-failure with a five-second delay is what turns a crash into an incident you can read about in the journal, instead of a service that quietly stopped at 03:00. Note that this one binds to loopback: the file above deliberately does not expose anything.

The bind address decides whether auth exists

There is a second decision, and it is not about process management at all: where the server listens. Bound to loopback, it is reachable only from the machine itself and no one else can form a connection to argue with. Bound to a routable address, it is reachable by anyone who can route to it, and authentication becomes the only thing between the internet and an agent that can run commands.

Treat the address as the security switch, because that is what it behaves like. Two workable shapes:

ShapeWhat listensWhat you have to add
Loopback only127.0.0.1 inside the distro or hostA tunnel or a reverse proxy from outside; no exposed port
Routable with authThe server on a real interfaceAuthentication, TLS in front, and a firewall rule you can explain

The first shape is not a compromise. For one operator on one machine it is the correct default, and it removes a whole category of problems that no amount of password strength fixes.

How you know it worked

Not “it replied once”. Three checks, and the middle one is the one people skip:

  1. Before the restart, note the process and the port: a listener and a parentless process. That is the baseline you will compare against.
  2. Restart the machine. Not the terminal, not the shell - the machine, or at minimum the distribution. This is the step that separates a service from a background command, and skipping it is how “unattended” turns out to mean “started this morning”.
  3. After the restart, ask the same two questions without touching anything: is the port listening, and does the unit report itself active. If you had to start it by hand, you have a habit, not a service.

Then one more, because a service that runs but cannot reach the network looks healthy until it is used: make the agent do something that requires an outbound request and read the log line it produces.

How to undo it

Everything here is reversible, which is why it is worth trying properly:

  • Stop the unit, disable it, then delete the file - in that order. Deleting the file first leaves the running process behind and is the usual reason a “removed” service keeps answering on the port.
  • Kill the background processes you started by hand before you trust the unit, otherwise you will have two of them and the port will be held by whichever started first.
  • Revert the bind address to loopback if you exposed it and do not need to. Restoring the old connection file on the client side completes the undo; the client keeps whatever values it read at launch, so it needs a restart too.

What we did not test

A real reboot of the Windows host with the distribution set to start automatically, a unit running as a non-root user with a proxy that requires a login shell, and the same setup on a plain VPS where the init system is the only init system. The measurements above are about a WSL2 distribution on one machine; the conclusion about shutdown is the one that repeats across all of them, and the unit file is the part that has to be tested where you intend to run it.

Questions people actually ask

Does nohup survive closing the terminal?

On this machine, yes - and so did setsid. Both processes were still there after the parent shell exited, reparented to another process in the distro rather than to init. If your goal is only to close the terminal, plain backgrounding is enough and the nohup-versus-setsid debate does not apply.

So why did they stop?

Because the whole distribution stopped. wsl --shutdown, a Windows reboot or a host restart takes the distro down and every process inside it with it, regardless of how it was started. That is the failure the usual advice does not address.

What should I use instead?

A unit file with a restart policy, run by the init system inside the distro. It starts on boot, restarts on failure, and its logs are somewhere you can find them later. On this machine systemd is PID 1 and reports running, so the mechanism is available; there is simply no unit installed for the agent.

Is it safe to expose the port?

Only with authentication and TLS in front. Binding to a routable address is what switches authentication on in the first place; leaving it on loopback and reaching it through a tunnel or a reverse proxy keeps the attack surface at zero without changing the app.

How do I know it will still be running tomorrow?

Restart the machine, then check the port and the unit state without touching anything else. If you have to start it by hand, it was never unattended - it was just forgotten.

Sources


Last checked: 2026-11-18 · The process listing, the two PIDs that outlived their parent shell, the wsl --shutdown result and the systemd readings were all taken on this machine on 2026-11-18.