This is the second article in a two-part series about a beginner’s guide to snaps in Linux. It covers how to run snaps from the command-line interface, creates and use snap aliases, interacts with a snap’s services, and create and manage snapshots of a snap.
Run Apps from Snaps
A snap may provide a single application (or a group of applications) which you run from the graphical user interface or using commands. By default, all applications associated to a snap are installed under the /snap/bin/ directory on Debian based distributions and /var/lib/snapd/snap/bin/ for RHEL based distributions.
You can list the content of the snap directory using the ls command as shown.
$ ls /snap/bin/ OR # ls /var/lib/snapd/snap/bin/
To run an app from the command-line, simply enter its absolute pathname, for example.
$ /snap/bin/mailspring OR # /var/lib/snapd/snap/bin/mailspring
To only type the application name without typing its full pathname, ensure that the /snap/bin/ or /var/lib/snapd/snap/bin/ is in your PATH environmental variable (it should be added by default).
You can verify the environmental variable by typing.
# echo $PATH
If the /snap/bin/ or /var/lib/snapd/snap/bin/ directory is in your PATH, you can run an app by just typing its name/command:
To see the commands available beneath a snap, run the “snap info snap-name” command, and look at the command section as highlighted in the following screenshot.
# snap info mailspring
You can also find the absolute pathname of an application or command using the which command.
# which mailspring
Create and Using Snap Aliases
Snap also supports creating aliases for applications. A snap’s default (or standard) aliases have to undergo a public review process before they are enabled, but you create aliases for your local system.
You can create an alias for a snap using the alias command.
# snap alias mailspring mls
To list aliases for a snap, for example, mailspring, run the following command. From now on, you can use the alias to run the snap.
# snap aliases mailspring
To remove an alias for a snap, use the unalias command.
# snap unalias mls
Managing a Snap’s Services
For some snaps, the underlying functionality is exposed through applications that run as daemons or services, once the snap is installed, they are automatically started to run continuously in the background. Besides, the services are also enabled to automatically start at system boot. Importantly, a single snap may contain several applications and services that work together to provide the overall functionality of that snap.
You can check the services for a snap under the service section in the output of the “snap info snap-name” command. For example, for rocketchat-server.
# snap info rocketchat-server
You can cross-check the services for a snap using the services command. The command output shows a service, whether it is enabled to automatically start at system boot, and whether it is active or not.
# snap services rocketchat-server
To stop a service from running, for example, rocketchat, use the stop command. Note that this action is not recommended, as manually stopping a snap’s service(s) may cause the snap to malfunction.
# snap stop rocketchat-server
To start a service, for example, rocketchat use the start command.
# snap start rocketchat-server
To restart a service after making some custom changes to the snap application, use the restart command. Note that all services for a specified snap will be restarted, by default:
# snap start rocketchat-server
To enable a service to automatically start at system boot time, use the enable command.
# snap enable rocketchat-server
To prevent a service from automatically starting at the next system boot, use the disable command.
# snap disable rocketchat-server
To view the logs for a service, use the log command using the -f option, which allows you to watch the logs on the screen in real-time.
# snap logs rocketchat-server OR # snap logs -f rocketchat-server
Important: You can run the above service commands both on individual snap’s services and on all services for a named snap, depending on the parameter provided. This means you can use a more specific service name if a snap has many services.
Creating and Managing a Snap’s Snapshots
Snapd stores a copy of the user, system, and configuration data for one or more snaps. You can trigger this manually or set it up to work automatically. This way, you can backup the state of a snap, revert it to a previous state as well as restore a fresh snapd installation to a previously saved state.
To manually generate a snapshot, use the “snap save” command. To create a snapshot for mailspring, run the following command:
# snap save mailspring
If no snap name is specified, snapd will generate snapshots for all installed snaps (add the
--no-wait option to run the process in the background to free up your terminal and allow you run other commands).
# snap save
To view the state of all snapshots, use the saved command. You can use the
--id flag to show the state of a specific snapshot:
# snap saved OR # snap saved --id=2
You can verify the integrity of a snapshot using the check-snapshot command and the snapshot identifier (set ID):
# snap check-snapshot 2
To restore the current user, system and configuration data with the corresponding data from a particular snapshot, use the restore command and specify the snapshot set ID:
# snap restore 2
To delete a snapshot from your system, use the forget command. Data for all snaps are deleted by default, you can specify a snap to only delete its data.
# snap forget 2 OR # snap forget 2 mailspring
This brings us to the end of this two-part series about a beginner’s guide to using snaps in Linux. For more information, especially about setting system options to customize your snap environment and so much more, see the Snap documentation. As usual, your questions or comments are welcome via the feedback form below.