Activate group with cron

If you have a social media blocklist and an intended usergroup. You can enable or disable the blocklist for the intended usergroup.

Disable Block:
0 18 * * 1-5  sudo sqlite3 /etc/pihole/gravity.db "DELETE FROM "adlist_by_group" WHERE group_id =<groupid>  and adlist_id = <adlistid>;"

Enable Block:
0 9 * * 1-5  sudo sqlite3 /etc/pihole/gravity.db "INSERT INTO "adlist_by_group" ("adlist_id","group_id") VALUES ('<adlistid>','<groupid>');"
1 Like

for get the id i need request the database ?


SELECT id FROM "group" WHERE name ='<groupname>'
SELECT id FROM "adlist" WHERE address ='<adlistaddress>'

It's a crude way of doing it, but I do it without problem for a similar purpose. I'd be happy to know if there is a more elegant way of doing it.

that's exactly what i was looking for thanks!

pi@raspberrypi:~ $ sudo sqlite3 /etc/pihole/gravity.db "SELECT id FROM "group" WHERE name ='Enfants_cours'"
Error: near "group": syntax error

sorry it's ok with simple quote

for me this is only necessary :smiley:

in ssh
edit the crontab

crontab -e

and past this rows : (0 min, 9am, all days, all month, only the week)

#Enable Block:
0 9 * * 1-5  sudo sqlite3 /etc/pihole/gravity.db "update 'group' set 'enabled'=1 where name='Enfants_cours';" ; /usr/local/bin/pihole restartdns reload-lists >/dev/null
#Disable Block:
0 18 * * 1-5  sudo sqlite3 /etc/pihole/gravity.db "update 'group' set 'enabled'=0 where name='Enfants_cours';" ; /usr/local/bin/pihole restartdns reload-lists >/dev/null


If the clients in your Enfants_cours don't belong to the default group, then disabling the Enfants_cours group will make them bypass the Pi-Hole altogether. It can have unintended consequences.

At least that's my understanding.
But if it works for you, all is well :slight_smile:

for exemple the device of my children:

today i enable with the site and it's ok. and my test with the sql get the same result in the site, i will check during the next week :wink:

I'll just re-iterate that disabling a group means Pi-Hole stops applying any rules to it. As long as that what you want it's all good. A simple solution is always better.

yes I understand and it is important to specify it.
Thank you for this additional information.

In my case, my devices have several groups and I have a specific group for school hours. so in my case it is the group that I deactivate. what I did so far by hand, and thanks to you it will be automatic.

thank you again for your help and for the speed of your response.


I am looking for such a soloution, too.

But I have issues regarding Browser caching. The blocked website is still resolvable from the browsers like Chrome and Firefox but not from the commandline via "nslookup".

Do you have simlilar issues and if not, how did you solve it?

Thank you all for the info. I used the above to enable disable a group based on time. For those looking for complete steps:

  1. Create a group: Group Management>Groups>Add. E.g. "School"
  2. Add clients to the Group Configuration: Group Management>Clients>Select Client>Add.
  3. Set the client to the newly created group in Group Assignment (i.e. "School"). Do not put it into the Default group.
  4. Add blacklist of domains: Group Management>Domains:Domain>Add to Blacklist. E.g. To block ALL of youtube, type in: and select Add domain as wildcard.
  5. Set the blacklist to the group: Under List of Entries set the Group Assignment to the new group (i.e. "School")
  6. Test it out:
    1. Enable the Group: Under Group Management>Groups, the Status should be Enabled.
    2. On the target host, open a browser in private mode and go to the blacklisted site. Confirm it is blocked.
    3. Disable the Group, and confirm that the target machine can visit the blacklisted site.
  7. You can do this manually whenever needed.

Automatic Scheduling
To set enable the block on regular intervals (e.g. 8am-2pm M-F)

  1. ssh to your pihole
  2. Edit the crontab file: crontab -e
  3. Add the following lines:
0 8 * * 1-5 sudo sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 1 where name = '<name of group>'" >> /tmp/pihole_group.log 2>&1
0 14 * * 1-5 sudo sqlite3 /etc/pihole/gravity.db "update 'group' set enabled = 0 where name = '<name of group>'" >> /tmp/pihole_group.log 2>&1

E.g. for group name School:

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

If you have any issues, check /tmp/pihole_group.log. If all is running correctly, the log file will be empty.


This thread is awesome. Is there a way to do this with an adlist instead? By updating the adlist table? I've got an adlist that blocks youtube and such, and it's applied to a couple of clients. I can enable and disable the adlist from the GUI and it works flawlessly. When I update the adlist table and set enabled = 0, I can see the list disabled in the GUI, but the domains still work. I've tried pihole restartdns reload-lists, but no dice.
I'm missing something. What is it?

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!