---
title: "systemd timer as a dead-man switch"
category: "code"
id: b2cd4749-ea54-469b-a82b-fecd77953c0b
canonical: https://solvedlist.com/solutions/systemd-timer-as-a-dead-man-switch
---

# systemd timer as a dead-man switch

> Solution for implementing a systemd service with monitoring that alerts when a nightly job fails to run or exits with errors

## Question

How to configure a systemd service and timer with a dead-man's-switch heartbeat for alerting on missed runs or failures

## Solution

Symptom: a nightly job (dump, sync, report) stops after a permission or mount change and nobody notices, because a job that never starts writes no error.

Cause: a crontab entry has no `network-online.target` dependency, no journal logging, no failure hook and no external watchdog. A skipped run leaves nothing behind.

Fix, one service plus one timer. `/etc/systemd/system/nightly-job.service`:
```
[Unit]
Description=Nightly maintenance job
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/nightly-job.sh
ExecStartPost=/usr/bin/curl -fsS -m 10 --retry 5 https://hc-ping.com/<ping-key>
ExecStopPost=/bin/sh -c '[ "$SERVICE_RESULT" = "success" ] || /usr/bin/curl -fsS -m 10 https://hc-ping.com/<ping-key>/fail'
```
`/etc/systemd/system/nightly-job.timer`:
```
[Timer]
OnCalendar=*-*-* 03:15
Persistent=true
RandomizedDelaySec=600

[Install]
WantedBy=timers.target
```
`systemctl daemon-reload && systemctl enable --now nightly-job.timer`

Check `$SERVICE_RESULT`, not `$EXIT_CODE`: `$EXIT_CODE` holds the word `exited` or `killed`, the numeric status sits in `$EXIT_STATUS`. Comparing `$EXIT_CODE` to "0" is always false and would send /fail after every successful run. Verified on systemd 257: a successful oneshot reports `success`, a failing one `exit-code`.

Verify: `systemctl list-timers nightly-job.timer`, `journalctl -u nightly-job.service -n 30 --no-pager`, and one manual `systemctl start` that must show up as a ping.

## Query directly via MCP

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}
```

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
