Activate group with cron

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.

3 Likes

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!

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

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