Skip to content

Instantly share code, notes, and snippets.

@warriordog
Last active April 25, 2024 16:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warriordog/8012155b6bc25684fe8d3431eb821e72 to your computer and use it in GitHub Desktop.
Save warriordog/8012155b6bc25684fe8d3431eb821e72 to your computer and use it in GitHub Desktop.
Snapd Management Scripts

Snapd Management Scripts

This gist contains some simple scripts that enable control over Snap's automatic updates.

Scripts

  • disable-snap.sh - Disables the snap service and blocks updates.
  • enable-snap.sh - Enables the snap service and allows updates and managing the snap package database.
  • update-snap.sh - Temporarily enables snap and updates all packages. Disables snap when finished.
  • install.sh - Installs all scripts as global commands.

Setup (blocking automatic Snap updates)

  1. Run chmod +x install.sh && ./install.sh to install the scripts to your computer.
  2. Run disable-snap to disable Snap and block updates.
  3. That's it, snap updates are now blocked!

To manually update snaps, run update-snap. To install new snaps or remove existing snaps, run enable-snap before executing your commands. When done, run disable-snap to block updates again.

Disclaimer / Caveat

The scripts in this gist are simple and implement a fairly crude and heavy-handed approach to disabling snap. As a result, it is impossible to run most snap commands while the snap service is disabled. For example, you cannot install a new snap package or even list installed packages while snapd is disabled. In some cases, applications that attempt to communicate with snapd may hang or crash rather than exit gracefully.

#!/bin/bash
# Disables the snap service.
#
# Author: Chris Koehler
# Updated: 2020-11-14
# Stop and disable snap daemon
sudo systemctl stop snapd.service
sudo systemctl mask snapd.service
#!/bin/bash
# Enables the snap service.
#
# Author: Chris Koehler
# Updated: 2020-11-14
# Enable and start snap deamon
sudo systemctl unmask snapd.service
sudo systemctl start snapd.service
#!/bin/bash
# Installs update-snap and related script as executable commands.
# Tested on Ubuntu 20.04 but should support any Linux system.
# Author: Chris Koehler
# Updated: 2020-11-14
echo Installing enable-snap...
sudo cp ./enable-snap.sh /usr/sbin/enable-snap
sudo chmod +x /usr/sbin/enable-snap
echo Installing disable-snap...
sudo cp ./disable-snap.sh /usr/sbin/disable-snap
sudo chmod +x /usr/sbin/disable-snap
echo Installing update-snap...
sudo cp ./update-snap.sh /usr/sbin/update-snap
sudo chmod +x /usr/sbin/update-snap
echo Done.
#!/bin/bash
# Manually updates snap-based packages.
# Automatically enables snapd, if disabled, and then disables it after applying updates.
# Adapted from https://askubuntu.com/a/1269770/705845.
#
# Service-related logs are recorded to "/var/log/update-snap.log".
# Check there in case of problems.
#
# Author: Chris Koehler
# Updated: 2020-11-14
# Enable and start snap deamon
echo Enabling snap service...
sudo sh -c 'enable-snap >> /var/log/update-snap.log 2>&1'
# Update packages
echo Updating snaps...
sudo snap refresh
# Stop and disable snap daemon
echo Disabling snap service...
sudo sh -c 'disable-snap >> /var/log/update-snap.log 2>&1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment