> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pgw.sipmicro.services/llms.txt
> Use this file to discover all available pages before exploring further.

# Run pgw under systemd

> Create a systemd unit for pgw, read its log with journalctl, and restart or reload it.

When you finish this page, pgw starts at boot, restarts after a failure, and writes its log to the journal. The unit uses the user and the files from [Install pgw on a server](https://docs.pgw.sipmicro.services/deploy/install.md), so follow that page up to its configuration file first.

## Create the unit

**1. Write the unit file**

Create `/etc/systemd/system/pgw.service`:

```ini theme={null}
[Unit]
Description=pgw SIP push gateway
After=network-online.target
Wants=network-online.target

[Service]
User=pgw
Environment=PGW_MASTER_KEY_FILE=/etc/pgw/master.key
EnvironmentFile=/etc/pgw/bootstrap.env
ExecStart=/usr/local/bin/pgw serve --config /etc/pgw/config.yaml
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
LimitNOFILE=1048576

[Install]
WantedBy=multi-user.target
```

| Line | Why |
| - | - |
| `Environment=PGW_MASTER_KEY_FILE` | pgw reads the master key from this file. pgw doesn't start when `PGW_MASTER_KEY` is set too. |
| `EnvironmentFile` | Sets `PGW_BOOTSTRAP_TOKEN`, which pgw reads only from the environment. The unit doesn't start when the file is missing. |
| `ExecReload` | `SIGHUP` makes pgw read its certificate files again. |
| `LimitNOFILE` | Each app connection takes one open file. See [Tune the host](https://docs.pgw.sipmicro.services/tune-the-host.md#raise-the-open-file-limit). |

**2. Enable and start pgw**

```bash theme={null}
sudo systemctl daemon-reload
sudo systemctl enable --now pgw
```

**3. Check that it started**

```bash theme={null}
journalctl -u pgw -n 20
```

The log ends with `pgw ready` and the addresses that pgw listens on. If it doesn't, the last line says why pgw didn't start.

## Read the log

systemd sends pgw's log to the journal:

```bash theme={null}
journalctl -u pgw
```

Each line carries a syslog priority, so `journalctl -p` filters by level. To show only warnings and errors:

```bash theme={null}
journalctl -u pgw -p warning
```

To change the log's level, format, or color, set `log_level`, `log_format`, or `log_color`. See [Settings](https://docs.pgw.sipmicro.services/reference/settings.md#logging).

## Restart or reload pgw

A reload makes pgw read a new certificate, and closes no connection. Every other change needs a restart.

| Command | What pgw does |
| - | - |
| `sudo systemctl reload pgw` | Reads its certificate files again. Open app connections stay. pgw reads no other setting on a reload. |
| `sudo systemctl restart pgw` | Stops, starts again, and reads its configuration. App connections close, and apps connect again when they next register or wake. |

You can restart pgw at any time, but calls that it is holding at that moment fail. When pgw stops, it answers those calls with `503`, saves the registrations in flight, and finishes the pushes that it already started. This takes up to about 15 seconds, and the last log line is `pgw stopped`. To stop pgw at once, send a second `SIGINT` or `SIGTERM`.

## Next steps

> [card] [Install a TLS certificate](https://docs.pgw.sipmicro.services/deploy/tls.md)
>
> Replace the self-signed certificate, which apps reject.

> [card] [Tune the host](https://docs.pgw.sipmicro.services/tune-the-host.md)
>
> Raise the UDP receive buffer and set the keepalive before many devices register.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.