How to Automatically Update R-Pi & Pi-Hole

Can I configure my Raspberry Pi, which I use only for Pi-hole, to automatically run sudo apt update -y && sudo apt upgrade -y and sudo pihole -up for updates without me having to do anything manually?

You can, but we don't recommend automatic updates for Pi-hole.

When you use Pi-hole, it becomes the DNS server of your local network.
This is an important service and it should not be offline or break without your knowledge.

If Pi-hole fails due to a failed automatic update, your network will break, but you won't notice until you need to use the Internet, or a family member complaints about connection issues.

This scenario is even worse when the failure happens in the middle of the night or when you are away.

Please rethink your workflow. Pi-hole software doesn't need to be updated very often.

An occasional manual update is usually a lot simpler/faster than fixing an occasional error caused by a bad automatic update.

For the first part there is: unattended-upgrades. But like already mentioned thinks might break.

The better way: reserve some minutes every sat/sun for upgrading all your systems (not only the pi).

Apart from warnings (true, keep in mind) and recommendations there's of course the opportunity to enter unattended jobs. The keyword is "cron".
Please consult your prefered search engine or AI to find the solution for your individual requirement.

The Pihole cron control file is /etc/cron.d/pihole. You can edit or better append the file with the jobs and timings by sudo nano /etc/cron.d/pihole in your terminal session (e.g. PUTTY).

To simplify the launch time you can easily use a cron generator like this

Take care and backup your system before editing system control files.

I used this command by getting help from AI. Is this correct?

sudo bash -c 'cat > /usr/local/sbin/pihole-auto-update.sh << "EOSCRIPT"
#!/bin/bash
set -e

LOG="/var/log/pihole-auto-update.log"

{
echo "===== $(date) ====="
apt-get update
apt-get -y upgrade
apt-get -y autoremove
pihole -up
echo
} >> "$LOG" 2>&1

EOSCRIPT

chmod +x /usr/local/sbin/pihole-auto-update.sh

cat > /etc/cron.d/pihole-auto-update << "EOCRON"
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

30 4 * * 0 root /usr/local/sbin/pihole-auto-update.sh
EOCRON'

Just test it :slight_smile:

It looks a little bit complex. No script is needed, you can just do:

sudo nano /etc/cron.d/pihole

Append both lines on the bottom of the text file

# Updates System und Distribution
`30 4 * * 0 root sudo apt-get -y update && sudo apt-get -y upgrade && sudo apt-get dist-upgrade && sudo apt-get -f install && sudo apt-get autoremove && sudo apt-get autoclean`

Ready.

I know it's been stated before, but this is a completely bad idea. Not only for Pi-hole itself but an apt upgrade could possibly install a new kernel. If the device is not rebooted and multiple upgrades happen, autoremoving old packages and kernels, the device could be running something no longer 'on disk'. A power fail or even scheduled reboot could end up with the device in a very bad, possibly unrecoverable state. It's not that hard to periodically check for updates. Heck Pi-hole's Admin UI even shows them at the bottom of each page. I also have something similar on monitoring pages I use for my RPi device. But, just like the others...this is only my opinion. Do you let your router auto update firmware?

Why encourage while every sane person and the developers advice against this?

You're gonna experience allot of pain if copying/pasting some AI generated code without exactly understanding what it does and knowing the implications!
And also if ignoring advice from the developers and more experienced users.

I recommend to create an account on Github, and enable e-mail notifications for new "Releases":

Watch > Custom > Releases

Or get notified via the RSS feed:

https://pi-hole.net/feed

That way you can read the changes first before deciding you need to upgrade.

Yes, that’s the standard procedure. It works on routers, smartphones, TVs, and usually on Raspberry Pi OS as well.

Most people are running a simple, non-commercial home network, and there’s no life-or-death risk involved in using auto-updates. If something goes wrong, you just fix it using a backup—which, of course, you have.

Your advice is certainly valid, but not everyone thinks the same way; some people want to try out their own modifications.

In my experience, my system has crashed only once in the last five years, and the culprit was Pi-hole, not OS updates.

So: Yes, go for it.

Because everyone here is curious in a way—wanting to gain their own experience and customize things. Nothing is more boring than plug-and-play.

No its not!

Yes there is!
Wait til you're out of the door and the spouse plus kids start complaining "The Internet is failing" and preparing to lynch you.

I agree you learn by mistakes.
But in this case it can be avoided.

.. and what do you do if someone doesn't want to avoid this?

I don't mind complaining kids. This time it's a digital detox day :wink:

Don't panic. It's just an ad blocker not your heating, not your oxygen supply.

I do my recommendation to not do it and move on.
But I dont encourage to do it anyway like you did ... if you feel a little bit of responsibility.

Its much more.
Everything hinges on DNS provided by Pi-hole.
Even your heating and oxygen supply if they are "smart" devices.

But why am I trying to convince you.
You seem to have already made up your mind.

But please dont drag other users into your scheme?
It might even result in more help requests here on Discourse and Github when things go south.

I don't disagree with your advices and recommendations.

But you also know me for quite some time. My view is the consumer view, the user view , you stand for expertise in coding and technical aspects.

Let the user decide on the software pimps. It's his machine, his installation, his problem. And by the way it's a request for an os update/enhancement which is in particular not a question of pihole.

There are thousands of bad practices that could break your setup.
But I try avoiding posting them here.
Monkey see monkey do.

Sadly a lot of Ubiquiti UniFi owners do it for all their networking stuff : Router/Switches/Accesspoints

And then they panic when everything goes wrong! :see_no_evil_monkey::rofl:

Update Check : Yes.
Actually Update the device too : No!

You always have to confirm manually :slight_smile:

Hehehe! Funny! :rofl:

Very often the users that need this kind of support become our problem in the end so watch out with giving people the wrong advice! :wink:

Okay, I think the situation is clear.

Linux pushes users (mostly coming from Windows) to get deepr in touch with Linux, specially with the complicated oldfashioned console/terminal. The first barriers are high, the commands complex. If successfull, users tend to put some automatism into the routines as not everybody sits on a consule every day. BTW, also W10/W11 updates automatically if you didn't deny that.
So I believe automatic updates are a regular user behavior.

On the other side the Linux experts, the techies, the coder. Everybody knows what to do, what are the potential risks, what the consequences. And everybody knows that only 10% is real code, the rest of 90% is a defend against the stupid user actions that kills all the nice stuff.

So users are to be expected to pimp their installation, but you are not expected to offer support on manipulated installations, especially on OS level.

Your advises are right, the consumers intension, too.
I don't mind any arguments. Things will happen, this way or another.

When I was still employed as a Linux admin, I noticed my Windows colleagues entering a change in the change management system whenever they needed to update the MS Exchange servers or the DC's etc ... scheduled and not automatic on release.
Same as I did for my Linux farm.

Not everyone should understand or I would be out of a job :wink:

Let's not involve stupid software into the mix :winking_face_with_tongue:

IMHO more of an exception than a rule...

LOL! Good one! :rofl: