A lightweight, cross-platform process watchdog. No third-party dependencies.
Checks if a target process is running every minute via cron/schtasks, and starts it if not.
- ✅ Cross-platform: Windows & Linux
- ✅ No third-party dependencies (stdlib only)
- ✅ Multi-task support via JSON config
- ✅ Process matching by cmdline substring (not just process name)
- ✅ PID file to prevent concurrent watchdog instances
- ✅ No console window flicker on Windows (
pythonw.exe)
Create watchdog.json in the same directory as watchdog.py:
{
"tasks": [
{
"cmd": "ssh -N -D 10081 root@myhost -p 2222 -o ServerAliveInterval=30 -o ServerAliveCountMax=3",
"match": "-D 10081",
"log": "/var/log/ssh-watchdog.log"
},
{
"cmd": "frpc -c /etc/frpc.ini",
"match": "frpc.ini"
}
]
}Then run:
python watchdog.pylog is optional — defaults to watchdog.log in the same directory.
python watchdog.py --cmd "ssh -N -D 10081 root@myhost" --match "-D 10081"Priority: CLI args > config file > defaults.
Run in PowerShell — no console window flicker:
# Register (every minute)
schtasks /create /tn "Watchdog" /tr '"C:\path\to\pythonw.exe" "C:\path\to\watchdog.py"' /sc minute /mo 1 /ru $env:USERNAME /f
# Run immediately
schtasks /run /tn "Watchdog"
# Query
schtasks /query /tn "Watchdog" /fo list
# Change interval (e.g. every 5 minutes)
schtasks /change /tn "Watchdog" /sc minute /mo 5
# Disable / Enable
schtasks /change /tn "Watchdog" /disable
schtasks /change /tn "Watchdog" /enable
# Delete
schtasks /delete /tn "Watchdog" /fUse
pythonw.exe(same directory aspython.exe) to avoid console window flicker.
crontab -eAdd:
* * * * * python3 /path/to/watchdog.py
2026-05-06 10:00:01 [OK] '-D 10081' already running (PID: 4321)
2026-05-06 10:01:01 [WARN] '-D 10081' not found, starting: ssh -N -D 10081 ...
2026-05-06 10:01:01 [OK] started successfully
--cmd Launch command (single task mode)
--match Substring to match against process cmdline
--log Log file path (default: watchdog.log next to script)
- Python 3.6+
- No pip installs needed