MacOS – How to prevent having to restart Safari when blocking sites via /etc/hosts file

macosmojaveNetworksafari

I have modified my /etc/hosts file to block website.com and www.website.com.

I know the formatting of my hosts file is correct because when I restart Safari, the block takes effect. I can also enter into a private window, and the block takes effect. If I do not take any action, Safari will eventually start blocking the sites from my host file after about 5 minutes. Is therany wayay I can expedite this?

When I test in Chrome, there is no need to restart Chrome, my blocks immediately start working.

I've attempted:

sudo killall -HUP mDNSResponder

and

sudo dscacheutil -flushcache

I've also read attempted all of the formatting suggestions on stack exchange for the etc/hosts file, but nothing seems to work. No matter what, Safari needs to be reset for the blocks to take place.

I would like for a solution that can be done programmatically (i.e. through a terminal command or some sort of script – not through some GUI application) so that I can package this in with my application.

Best Answer

I've discovered that if I disconnect from wifi for about 10 seconds and then reconnect, that will force safari to read the hosts file correctly. Anyone know what type of data gets cleared when you disconnect and reconnect to wifi? Maybe that can guide me to a workaround.