# Activate group with cron

**URL:** <https://discourse.pi-hole.net/t/activate-group-with-cron/32660>\
**Category:** Community Help\
**Tags:** v5-0\
**Created:** [May 16, 2020, 11:18pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660 "2020-05-16T23:18:46Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![steve-o](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/steve-o/32/19610_2.png) [@steve-o](https://discourse.pi-hole.net/u/steve-o)\
**Post date:** [November 11, 2020, 6:29pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/22 "2020-11-11T18:29:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skroesen](https://discourse-cdn.pi-hole.net/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@skroesen](https://discourse.pi-hole.net/u/skroesen)\
**Post date:** [December 8, 2020, 9:00pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/23 "2020-12-08T21:00:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Scope666](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/scope666/32/19437_2.png) [@Scope666](https://discourse.pi-hole.net/u/Scope666)\
**Post date:** [December 8, 2020, 10:43pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/24 "2020-12-08T22:43:25Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![skroesen](https://discourse-cdn.pi-hole.net/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@skroesen](https://discourse.pi-hole.net/u/skroesen)\
**Post date:** [December 8, 2020, 11:49pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/25 "2020-12-08T23:49:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GregRN](https://discourse-cdn.pi-hole.net/letter_avatar_proxy/v4/letter/g/7ea924/32.png) [@GregRN](https://discourse.pi-hole.net/u/GregRN)\
**Post date:** [January 5, 2021, 5:42pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/26 "2021-01-05T17:42:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![GregRN](https://discourse-cdn.pi-hole.net/letter_avatar_proxy/v4/letter/g/7ea924/32.png) [@GregRN](https://discourse.pi-hole.net/u/GregRN)\
**Post date:** [January 5, 2021, 6:29pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/27 "2021-01-05T18:29:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amitsax](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/amitsax/32/16381_2.png) [@amitsax](https://discourse.pi-hole.net/u/amitsax)\
**Post date:** [January 25, 2021, 1:27am UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/28 "2021-01-25T01:27:16Z")

</div>

Can we leverage client to group assignment mapping - to enable/disable applicable blocking lists?  
[Pi-hole](http://pi.hole/admin/groups-clients.php)  
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.

---

<div class="post-metadata">

**Author:** ![jfb](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/jfb/32/4332_2.png) [@jfb](https://discourse.pi-hole.net/u/jfb)\
**Post date:** [January 25, 2021, 2:11am UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/29 "2021-01-25T02:11:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amitsax](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/amitsax/32/16381_2.png) [@amitsax](https://discourse.pi-hole.net/u/amitsax)\
**Post date:** [January 25, 2021, 2:20am UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/30 "2021-01-25T02:20:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jfb](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/jfb/32/4332_2.png) [@jfb](https://discourse.pi-hole.net/u/jfb)\
**Post date:** [January 25, 2021, 2:22am UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/31 "2021-01-25T02:22:41Z")

</div>

> **[Redirecting...](https://docs.pi-hole.net/database/gravity/)**

---

<div class="post-metadata">

**Author:** ![Ltek](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/ltek/32/24624_2.png) [@Ltek](https://discourse.pi-hole.net/u/Ltek)\
**Post date:** [April 7, 2021, 2:01pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/32 "2021-04-07T14:01:22Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![milkywade](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/milkywade/32/27354_2.png) [@milkywade](https://discourse.pi-hole.net/u/milkywade)\
**Post date:** [September 22, 2021, 2:41pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/33 "2021-09-22T14:41:49Z")

</div>

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:

> [@Feature request - Automate changes to client group assignment](https://discourse.pi-hole.net/t/feature-request-automate-changes-to-client-group-assignment/49805):
>
> Hi - I manage my small children's internet access using the client + group assignment feature. Whenever my children ask me for unrestricted internet access (eg to play a game online), I have to manually change the group their client has been assigned to. Life would be much simpler if I could automate such changes whereby I could tell to my children that they have unrestricted access from 5-6pm and only at that time. Examples of automation: allow CRON system script to change groups (personal…

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

---

<div class="post-metadata">

**Author:** ![hektormisplon](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/hektormisplon/32/29233_2.png) [@hektormisplon](https://discourse.pi-hole.net/u/hektormisplon)\
**Post date:** [January 15, 2022, 11:35pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/34 "2022-01-15T23:35:29Z")

</div>

This is what I ended up using:

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

```

The `pihole-toggle-block` script looks like this:

```auto
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.

---

<div class="post-metadata">

**Author:** ![rdwebdesign](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/rdwebdesign/32/51363_2.png) [@rdwebdesign](https://discourse.pi-hole.net/u/rdwebdesign)\
**Post date:** [October 19, 2022, 2:49am UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/35 "2022-10-19T02:49:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pobrecito](https://discourse-cdn.pi-hole.net/letter_avatar_proxy/v4/letter/p/57b2e6/32.png) [@pobrecito](https://discourse.pi-hole.net/u/pobrecito)\
**Post date:** [November 14, 2023, 6:11pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/36 "2023-11-14T18:11:40Z")

</div>

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

```auto
# 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.

```auto
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:

```auto
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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Scope666](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/scope666/32/19437_2.png) [@Scope666](https://discourse.pi-hole.net/u/Scope666)\
**Post date:** [November 14, 2023, 7:45pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/37 "2023-11-14T19:45:01Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![piholezero](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/piholezero/32/46024_2.png) [@piholezero](https://discourse.pi-hole.net/u/piholezero)\
**Post date:** [March 2, 2025, 5:14pm UTC](https://discourse.pi-hole.net/t/activate-group-with-cron/32660/38 "2025-03-02T17:14:30Z")

</div>

FYI: after years of working fine, this stopped working after upgrading to pihole v6 since it removed `restartdns`. Seems to be replaced with `reloaddns`.

You will need to update your crontab.

> **[pi-hole/pi-hole v6.0 on GitHub](https://newreleases.io/project/github/pi-hole/pi-hole/release/v6.0)**
>
> New release pi-hole/pi-hole version v6.0 on GitHub.

> Remove the `restartdns` functionality and promote the `reloaddns` functions by [@PromoFaux](https://github.com/PromoFaux) in [#5780](https://github.com/pi-hole/pi-hole/pull/5780)

[Previous page](https://discourse.pi-hole.net/t/activate-group-with-cron/32660.md?page=1)
