Skip to content

Instantly share code, notes, and snippets.

@rubo77
Last active March 31, 2023 16:46
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubo77/15366925051dd214b18c306f9389a573 to your computer and use it in GitHub Desktop.
Save rubo77/15366925051dd214b18c306f9389a573 to your computer and use it in GitHub Desktop.
start snap service, delete snap backups, do a refresh and disable it again
#!/bin/bash
# this script starts the snapd service, deletes old snap images, does a refresh and disables it again
# put it in /usr/local/sbin/snap-update and give it executable rights
# Note: If you run apt upgrade and there is an update to a package that is managed by snapd,
# the upgrade will hang if snapd is still masked, so remember to unmask snapd in that case
# and start apt upgrade again
set -x
systemctl unmask snapd.service
systemctl start snapd.service
systemctl status --no-pager snapd.service
snap refresh
LANG=C snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
done
sudo rm /var/lib/snapd/cache/*
systemctl mask snapd.service
systemctl stop snapd.service
sleep 2
kill -9 $(pgrep snapd)
@rubo77
Copy link
Author

rubo77 commented Jan 13, 2020

@rubo77
Copy link
Author

rubo77 commented Jan 13, 2020

useful, if you have lots of traffic due to snapd update, see nethogs

@csaltos
Copy link

csaltos commented Jan 24, 2021

wonderful, thanks for the script, it's cool !!

@drhuh
Copy link

drhuh commented Apr 12, 2021

I like this script, but there is one drawback: after stopping snapd, I am not able to start applications which have been installed via snap, not all, but for instance opera or chromium browser:

$ opera
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
error: cannot communicate with server: Post http://localhost/v2/snapctl: dial unix /run/snapd-snap.socket: connect: no such file or directory ERROR: not connected to the gnome-3-28-1804 content interface.

Other applications like pycharm or spotify still work.

@rubo77
Copy link
Author

rubo77 commented Apr 14, 2021

@drhuh maybe Opera and chromium try to open swinging from cache?
Try removing this line

sudo rm /var/lib/snapd/cache/*

And what's the error on Chrome?

Maybe create a chromium start script that enables snapd if you start it and stop/mask it with you end it

@drhuh
Copy link

drhuh commented Apr 18, 2021

@drhuh maybe Opera tries to open swinging from cache?
Try removing this line

sudo rm /var/lib/snapd/cache/*

And what's the error on Chrome?

Maybe create an opera start Script that enables snap of you start opera and support it with you end it

This does not help, here the output after removing the sudo rm ... line:
$ sudo bin/snap-update.sh
+ systemctl unmask snapd.service
Removed /etc/systemd/system/snapd.service.
+ systemctl start snapd.service
+ systemctl status --no-pager snapd.service
● snapd.service - Snap Daemon
Loaded: loaded (/lib/systemd/system/snapd.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-04-18 19:14:39 CEST; 5ms ago
TriggeredBy: ● snapd.socket
Main PID: 686088 (snapd)
Tasks: 16 (limit: 28344)
Memory: 16.3M
CGroup: /system.slice/snapd.service
├─686088 /usr/lib/snapd/snapd
└─686133 udevadm info -e

Apr 18 19:14:39 t490huh systemd[1]: Starting Snap Daemon...
Apr 18 19:14:39 t490huh snapd[686088]: AppArmor status: apparmor is enabled and all features are available
Apr 18 19:14:39 t490huh snapd[686088]: AppArmor status: apparmor is enabled and all features are available
Apr 18 19:14:39 t490huh snapd[686088]: daemon.go:347: started snapd/2.49.2 (series 16; classic) ubuntu/20.04 (amd64) linux/5.8.0-48-generic.
Apr 18 19:14:39 t490huh snapd[686088]: daemon.go:440: adjusting startup timeout by 1m30s (pessimistic estimate of 30s plus 5s per snap)
Apr 18 19:14:39 t490huh systemd[1]: Started Snap Daemon.
+ snap refresh
All snaps up to date.
+ LANG=C
+ snap list --all
+ awk '/disabled/{print $1, $3}'
+ read snapname revision
+ systemctl mask snapd.service
Created symlink /etc/systemd/system/snapd.service → /dev/null.
+ systemctl stop snapd.service
+ sleep 2
++ pgrep snapd
+ kill -9
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

Obviously the last kill command does not find a running snap process.

If I start chromium, I get this output:
$ chromium-browser
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
error: cannot communicate with server: Post http://localhost/v2/snapctl: read unix @->/run/snapd-snap.socket: read: connection reset by peer
ERROR: not connected to the gnome-3-28-1804 content interface.

In the meantime I removed the snap version of Opera and replaced it by the debian package which can be downloaded from the opera home page, therefore I cannot test the output of opera again.

@rubo77
Copy link
Author

rubo77 commented Apr 21, 2021

#!/bin/bash
# this script starts the snapd service, chromium and disables snap again                                      
# put it in /usr/local/sbin/chromium-snap-start and give it executable rights

set -x
sudo systemctl unmask snapd.service
sudo systemctl start snapd.service
chromium    
sudo systemctl mask snapd.service
sudo systemctl stop snapd.service

@drhuh
Copy link

drhuh commented May 14, 2021

#!/bin/bash
# this script starts the snapd service, chromium and disables snap again                                      
# put it in /usr/local/sbin/chromium-snap-start and give it executable rights

set -x
sudo systemctl unmask snapd.service
sudo systemctl start snapd.service
chromium    
sudo systemctl mask snapd.service
sudo systemctl stop snapd.service

Sorry, but this became too complicated for me. Now I removed snap at all from my Ubuntu machine and switched to other package locations. I will switch back to snap once there is an option to disable automatic updates ;-) . Thank you for your support so far!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment