Activate group with cron

Hello, I'm looking for a way to activate a group from 9 a.m. to 6 p.m. and have it deactivated from 6:01 p.m. to 8:59 a.m.
with school at home I want to block social media during school hours.
Thank you in advance for your help.

1 Like

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

thank,
for get the id i need request the database ?

Yeah:

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

1 Like

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:
image

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.

amicably
thomas

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:
Configuration

  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: youtube.com 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.

2 Likes