Using cron to enable / disable pihole?

I've been using pihole to block facebook & twitter on my home wifi. I only want this block to operate between 10pm and 6am, when we should all be sleeping.

I've set up a crontab like so:

0 22 * * * PATH="$PATH:/usr/local/bin/" pihole enable
0 6 * * * PATH="$PATH:/usr/local/bin/" pihole disable

and pihole status reports enabled after 10pm as expected, so it would appear the cron job is working.

However, I don't actually see my network behaving as if pihole is enabled: browsers continue to access fb & twitter, and (for instance) dig facebook.com returns

facebook.com.		61	IN	A	157.240.1.35

e.g the DNS still resolves to the real server.

I do see things behaving as expected if I manually toggle Enabled / Disabled in the Pihole dashboard.

So, a question: should running pihole enable on the commandline and using the toggle on the dashboard theoretically do exactly the same thing? Why might it not do?

Are you using pi-hole as your sole DNS on your clients/router?

Enable/Disable should work as expected however if you use a secondary (non pi-hole DNS) your queries might go through that one ...

what's the output of nslookup facebook.com when blocking should be enabled?

I've just tried running pihole enable / disable manually at the CLI, and everything works as expected (browser and dig). So I guess it is a cron issue after all.

try setting up the PATH before the actual crontab:

PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
0 22 * * * pihole enable
0 6 * * * pihole disable

Would not client DNS caching come into play here? Disabling pihole after a client has already resolved the domain, means it might not ask again for some time. It doesn't need to.

This strongly depends on your clients setup. Chrome, for example, has an internal DNS chace. Firefox, however, didn't had such a cache when I checked last time. It also depends on the operating system as many Linux systems come with a locally running caching DNS server such as dnsmasq which is doing nothing else than caching...

Which user is executing this cron rule? It should be root.

I agree. I am mostly pointing out that disabling/enabling pihole may not be completely effective if client caching is in play.

Can confirm that, on my Mac at least, Chrome and Firefox behave as @DL6ER says. This is why I was using dig to test, which seems to do fresh DNS queries.

Aha! I had been setting the cron rule using sudo crontab -e when logged in as pi on the Raspberry Pi running pihole; my assumption was that the sudo would mean that I was editing the crontab for root, but I now think I was wrong about that.

Editing /etc/crontab directly and specifying root as the user for the cron rules seems to have worked. Hoorah! Thanks, @DL6ER