> For the complete documentation index, see [llms.txt](https://docs.zongsoft.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zongsoft.com/documentation/en/hosting/daemon.md).

# Background Service Host

Host long-term background tasks, correctly distinguishing between host lifecycle integration, service installation and business readiness.

The background service host is used for long-term work such as message consumption, scheduled tasks, and synchronization processing. The business is started and stopped through the plugin [Workers](/documentation/en/framework/core/components/worker.md) integration, and there is no need to write the loop directly into the host entry.

## Platform Lifecycle

The current entry uses `Application.Daemon("zongsoft.daemon", ...)` and passes in `host=daemon` and `site=daemon`. Integrate Windows Service or systemd depending on the build platform.

Source: [hosting/daemon/Program.cs](https://github.com/Zongsoft/hosting/blob/main/daemon/Program.cs#L9) (excerpt; see source for context).

{% code title="Program.cs" %}

```csharp
static void Main(string[] args)
{
	#if WINDOWS
	Zongsoft.Plugins.Hosting.Application
		.Daemon("zongsoft.daemon", [.. args, "host=daemon", "site=daemon"], builder =>
		{
			builder.Services.AddWindowsService(options => options.ServiceName = builder.Environment.ApplicationName);
		}).Run();
	#elif LINUX
	Zongsoft.Plugins.Hosting.Application
		.Daemon("zongsoft.daemon", [.. args, "host=daemon", "site=daemon"], builder =>
		{
			builder.Services.AddSystemd();
		}).Run();
	#else
	Zongsoft.Plugins.Hosting.Application
		.Daemon("zongsoft.daemon", [.. args, "host=daemon", "site=daemon"])
		.Run();
	#endif
}
```

{% endcode %}

These calls make the host respond to the system service lifecycle and do not automatically install the service in the operating system. Service registration, startup account, working directory, automatic startup and recovery strategy are completed by the installation package or operation and maintenance configuration.

## Integration Order

1. Verify the business configuration and handler in [Terminal Host](/documentation/en/hosting/terminal.md).
2. Prepare the daemon release directory and check the Main, daemon scheme and business plugins.
3. Start the deployed program in the foreground and confirm that there is no logic that relies on terminal input.
4. Register the service through the installation process that conforms to the target system, and check the actual running account and path.
5. Verify business readiness, recovery after stopping and restarting, rather than just checking that the service status is Running.

## How Workers Should Run

The startup phase completes the necessary initialization, the continuous loop should respond to cancellation, and avoid unbounded creation of background tasks. When external services are temporarily unavailable, there should be a clear reconnection or failure strategy; exceptions cannot be swallowed and the process appears to be healthy but is no longer processing work.

In the stop phase, reception is stopped first, and then in-transit tasks are processed within the allowed time. Message consumption needs to be combined with [Confirm with idempotent](/documentation/en/framework/messaging/reliability.md), and scheduled jobs need to be combined with [Scheduling lifecycle](/documentation/en/framework/externals/execution.md).

## Installation and Account

The Windows directory provides `install.cmd` and `uninstall.cmd`; the Linux installation package uses [Packaging Tool](/documentation/en/tools/packager.md) to generate service files and lifecycle scripts. Check the service name, program path and data directory before execution.

{% hint style="warning" %}
🚨 Installation/uninstallation will modify the system service configuration. The environment variables, user directories, certificates, and network permissions seen by the service account may differ from those seen by the interactive account and must be accessed using actual service authentication-dependent access.
{% endhint %}

## Common Faults

When starting and exiting, check entry, runtime, assembly and configuration loading errors. When the process is alive but not working, check whether the [worker](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/src/Components/IWorker.cs) is mounted, whether the startup fails, and whether the message subscription or job storage is normal. Only when there is an error in the service environment, focus on comparing the working directory, account and environment configuration.

Logs and persistent data should have independent paths and permissions. Especially when using [Full upgrade](/documentation/en/framework/upgrading.md), in-application data cannot be regarded as retained content by default.

Source references: [Backend host](https://github.com/Zongsoft/hosting/tree/main/daemon).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zongsoft.com/documentation/en/hosting/daemon.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
