Activate group with cron

When you set enabled = 0 you disabled the adlist. It is expected that the domain works thereafter (it is not blocked).

I had a similar problem where the cron job disabling my "Block" group wouldn't actually stop blocking clients, the GUI showed the change, but the behavior wouldn't change until I toggled the Enabled / Disabled button for my "Block" group in the GUI or restarted DNS, so I landed on this which is working for me:

0 23 * * 0-4 sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 1 where name = 'Block'" >> /tmp/pihole_group.log 2>&1
0 6 * * 1-5 sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 0 where name = 'Block'" >> /tmp/pihole_group.log 2>&1
1 6 * * 1-5 PATH="$PATH:/usr/sbin:/usr/local/bin/" pihole restartdns >> /tmp/restartpihole.log 2>&1

This is required as said in the documentation.

I was going by what piDiddy posted above. His example doesn't show that so thought I would offer it for anyone else trying to do it via Cron.

1 Like

I think I messed up my original post. I think I meant "still DON'T work". My bad!

What was the command you used to enable/disable an adlist?

Follow PiDiddy's guide above, my crontab under the root user looks like this. It's been working really well for weeks now:

0 23 * * 0-4 sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 1 where name = 'Block'" >> /tmp/pihole_group.log 2>&1

1 23 * * 0-4 PATH="$PATH:/usr/sbin:/usr/local/bin/" pihole restartdns >> /tmp/restartpihole.log 2>&1

0 6 * * 1-5 sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 0 where name = 'Block'" >> /tmp/pihole_group.log 2>&1

1 6 * * 1-5 PATH="$PATH:/usr/sbin:/usr/local/bin/" pihole restartdns >> /tmp/restartpihole.log 2>&1
1 Like

Can that be adjsuted for an adlist, or is that for an adlist? I am not currently doing anything in groups in my pihole setup.

Thanks for posting out the instructions. Question, when the sqlite3 command is run, I get an error - "attempt to write a readonly database". When I check the group that should be enabled, it is not.

I'm guessing from other posts that it's because the gravity.db is owned by pihole and not root (correct me if I'm wrong on this). Can somebody tell me the steps/commands to correct this error?

I figured it out. I didn't add the "sudo" prior to sqlite3. Working now!

Can we leverage client to group assignment mapping - to enable/disable applicable blocking lists?
Pi-hole
Above methods are using enable/disable of lists themselves.
I would prefer to not change the status of lists or groups but rather remove or apply their assignment to specific clients to get the blocking results.

Use the same technique but change the SQL command to alter group assignments.

Cool.. What's the group assignment table name?

https://docs.pi-hole.net/database/gravity/

1 Like

We need to have this in the native UI (without needing a hack). To simply set a timeframes to Groups...

"Apply Blacklist - during time frame"

"Block / Disable all activity - during time frame"

1 Like

Many thanks for the cron hacks guys, they are sent from heaven.

Not knowing about those I opened this feature request a few days ago:

You can use it to decide whether this functionality is something worth upvoting to become part of pi-hole.

1 Like

This is what I ended up using:

0 8  * * * /home/pi/pihole-toggle-block
0 22 * * * /home/pi/pihole-toggle-block

The pihole-toggle-block script looks like this:

block_status="$(docker exec ad0 sqlite3 /etc/pihole/gravity.db "select enabled from 'group' where name = 'Block';")"
((block_status ^= 1))

docker exec ad0 sqlite3 /etc/pihole/gravity.db \
  "update 'group' set enabled="$block_status" where name='Block'" >> /tmp/pihole_group.log 2>&1

Hope this helps.

1 Like

Just a note on your idea, if someone wants to use it:

Your pihole-toggle-block always inverts the enable status every time it runs.
This idea works fine if ALL attempts to run the command are successful, but if one fails the result will be inverted.

Example:
You want to disable at 8:00 and enable at 22:00.

  • At 7:59 the status is enabled.
  • At 8:00 your cron job fails (for whatever reason). The status still is enabled.
  • At 22:00 the cron job runs correctly and the status become disable, and so on...

The result will be the opposite of what you expect.

I have been using this approach and I find that it doesn't always work. Maybe someone can tell me what I am doing wrong?

I want to completely block my kids from accessing the internet between 12:30 am and 5:30 am. I have a blacklisted regex domain ".*" that is assigned to four groups (one kill-internet group for each kid, and all of each kid's devices are assigned to the relevant group). So when the group is enabled, any IP address lookup from any device in the group should be blocked.

The cron jobs look like this (yes, it's the root-owned crontab):

# m h  dom mon dow       command
30 00  *   *   *         sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 1 where id in (14, 15, 16, 17)" >> /tmp/pihole_group.log 2>&1
30 05  *   *   *         sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 0 where id in (14, 15, 16, 17)" >> /tmp/pihole_group.log 2>&1

and they appear to have run just fine last night.

pi@pi3bplus:# cat syslog | grep CRON | grep sqlite
Nov 14 00:30:01 pi3bplus CRON[11309]: (root) CMD (sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 1 where id in (14, 15, 16, 17)" >> /tmp/pihole_group.log 2>&1)
Nov 14 05:30:01 pi3bplus CRON[5788]: (root) CMD (sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 0 where id in (14, 15, 16, 17)" >> /tmp/pihole_group.log 2>&1)

/tmp/pihole_group.log is empty, so I take it there were no errors.

Here are the four groups from the sqlite database:

sqlite> select * from "group" where id in (14, 15, 16, 17);
14|0|Kill-C|1665611517|1699982472|Kill C internet
15|0|Kill-E|1665611529|1699982472|Kill E internet
16|0|Kill-A|1665611538|1699982473|Kill A internet
17|0|Kill-I|1665611548|1699982473|Kill I internet

and here you can see the .* domain assigned to each of those groups.

sqlite> select a.group_id, b.* from domainlist_by_group a inner join domainlist b where a.group_id in (14,15,16,17) and a.domainlist_id = b.id;
14|149|3|.*|1|1646338550|1666643098|Kill Switch
15|149|3|.*|1|1646338550|1666643098|Kill Switch
16|149|3|.*|1|1646338550|1666643098|Kill Switch
17|149|3|.*|1|1646338550|1666643098|Kill Switch

And yet, one of my kids was on reddit from 12:30 until 2 am last night. To troubleshoot, I turned on the kill-group for her devices this morning (in the pihole GUI), and I can't access any internet site from her laptop. So I can't figure out what happened last night.

I've checked this before, and the code in the cron job changes the 'enabled' value from 0 to 1 or 1 to 0 exactly as expected when I query before and after running it.

I have no idea.

Check my post above, you have to reload pihole after the change.

1 23 * * 0-4 PATH="$PATH:/usr/sbin:/usr/local/bin/" pihole restartdns >> /tmp/restartpihole.log 2>&1
1 Like