ThisCalculator

Everyday

Pi-hole Bedtime Blocking: How to Automate a Nightly Schedule

Updated · Standard formula, shown below

To automate a nightly Pi-hole blocking schedule, you use the built-in API endpoint api.php?block and api.php?unblock combined with your router’s or a device’s cron job (scheduled task). You set two times—one to enable blocking (e.g., 10:00 PM) and one to disable it (e.g., 7:00 AM)—and the system runs the corresponding command automatically each day.

Background: Why Block DNS at Night?

Pi-hole is a network-wide ad blocker that works as a DNS sinkhole. It intercepts DNS queries from all devices on your LAN and returns a null answer for domains on its blocklist. While many people run it 24/7, there are good reasons to pause blocking during certain hours:

  • Parental control: Restrict internet access for kids’ devices after bedtime, but allow it for adults or work devices.
  • Workarounds: Some streaming services or smart home devices misbehave when DNS is filtered; a nightly block window doesn’t help, but a “quiet hours” schedule might be used for a different purpose.
  • Maintenance: You might want to allow updates or backups to run overnight without interference.

The most common use is to enforce a “lights out” policy—blocking all internet (or at least ad-heavy sites) for a set period. This is a lightweight alternative to full router-based parental controls.

How Pi-hole Blocking Works

Pi-hole has two states: enabled (blocking active) and disabled (all DNS queries pass through). You can toggle the state manually via the web interface, but for automation you use the API.

The relevant endpoints are:

  • http:///admin/api.php?block – enables blocking
  • http:///admin/api.php?unblock – disables blocking
  • http:///admin/api.php?enable – same as block (alias)
  • http:///admin/api.php?disable – same as unblock (alias)

You can also specify a duration: ?disable=60 disables for 60 seconds. But for a nightly schedule, you want absolute on/off commands.

Choosing the Scheduling Tool

You have three main options to run the API calls on a schedule:

  1. Router’s built-in scheduler – Many routers (e.g., Asus, DD-WRT, OpenWrt) have a cron or “custom script” feature. You can add two cron entries.
  2. Pi-hole’s own cron – Since Pi-hole runs on a Linux system (Raspberry Pi, Debian, etc.), you can add a crontab entry directly on that device.
  3. External device – A separate computer, NAS, or smart home hub (like Home Assistant) can call the API at set times.

For most users, using the Pi-hole device’s own cron is simplest because it doesn’t depend on another device being on.

Step-by-Step: Setting Up a Nightly Schedule with Cron

Here’s how to automate a 10:00 PM block and 7:00 AM unblock using cron on the Pi-hole system.

  1. SSH into your Pi-hole device (e.g., ssh pi@192.168.1.100).
  2. Edit the crontab: crontab -e
  3. Add these two lines (adjust times as needed):
    0 22 * * * curl -s "http://localhost/admin/api.php?block" >/dev/null 2>&1
    0 7 * * * curl -s "http://localhost/admin/api.php?unblock" >/dev/null 2>&1
  4. Save and exit. Cron will run the commands daily.

If your Pi-hole uses a custom port or you access it via a domain, replace localhost with the appropriate address. You may also need to include an authentication token if you have API auth enabled (see “Common Problems”).

Common Problems and Troubleshooting

  • API authentication: If you enabled “API authentication” in Pi-hole settings, you must append &auth=YOUR_APP_TOKEN to the URL. Find the token in Settings → API/Web interface → Show API token.
  • curl not installed: If you get “command not found”, install curl with sudo apt install curl or use wget instead.
  • Blocking does not toggle: Check that the URL returns {"status":"enabled"} or similar. If you get a 401 error, the auth token is wrong.
  • Router cron vs. Pi cron: If you use a router that powers off at night, the Pi’s cron is more reliable.
  • Daylight saving time: Cron uses the system time, which may change with DST. If you want a fixed UTC schedule, use CRON_TZ=UTC at the top of crontab (not all systems support it).

Alternatives: Using a Script for More Control

If you need more than just on/off—like blocking only certain devices or only on weekdays—you can write a small shell script. For example, to block only on weekdays:

#!/bin/bash
if [ $(date +%u) -le 5 ]; then
  curl -s "http://localhost/admin/api.php?block" >/dev/null
fi

Then call that script from cron. You can also use Pi-hole’s “Group Management” to create a group for kids’ devices and block that group only, but that requires more complex API calls (using the API’s ?group= parameter is not officially documented, so stick to the simple block/unblock).

Costs and Safety Considerations

There is no monetary cost—Pi-hole is free and open source. The only “cost” is the electricity for the device running it, which is minimal on a Raspberry Pi (a few watts).

Safety-wise, remember that blocking DNS only affects devices that use Pi-hole as their DNS server. If a device has a hardcoded DNS (e.g., some smart TVs), it will bypass the block. Also, if you block all DNS, devices may show “no internet” but they can still connect via IP addresses, so this is not a security boundary—it’s a convenience/parental control.

Frequently Asked Questions

Can I set a different schedule for weekends?

Yes. Cron supports day-of-week fields. For example, 0 22 * * 1-5 blocks at 10 PM Monday to Friday, and 0 23 * * 6,0 blocks at 11 PM on Saturday and Sunday. Add separate lines for each schedule.

What if I want to temporarily override the schedule?

You can manually toggle via the web interface or run the API command manually. The next cron job will override your manual change at the next scheduled time. If you want a permanent override, you’d need to comment out the cron lines.

Does the schedule affect the Pi-hole’s own updates?

No, the API only toggles DNS blocking. Pi-hole updates and system updates run independently. However, if you block DNS, the Pi-hole itself may not be able to resolve domain names for updates, so schedule updates during unblocked hours.

Can I block only certain devices at night?

Yes, but not with the simple block/unblock API. You would need to use Pi-hole’s API to disable blocking only for specific clients, which is not officially exposed. A workaround is to create a separate VLAN or network for kids’ devices and run Pi-hole as DNS for that network only, then block/unblock that Pi-hole instance.

Is there a way to do this without cron?

Yes, you can use systemd timers, a script that runs continuously and checks the time, or a third-party app like Home Assistant. Cron is the most common and lightweight method.

What if my Pi-hole is not on the same device as the scheduler?

That’s fine. The API is HTTP-based, so any device on your network can call it. Just replace localhost with the Pi-hole’s IP address, and ensure the API is accessible (you may need to set the API to “allow all origins” or use the token).

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.