Managing Services with Snap on Ubuntu

Table of Contents

Managing Services with Snap on Ubuntu

On Ubuntu, if you want to use snap start commands to run a program as a backend service that automatically restarts when it exits, you'll need to create and package your application as a Snap with proper service definitions.

Creating a Snap with Service Capabilities

1. Create a snapcraft.yaml file

The key to making your application run as a service through Snap is defining it properly in your snapcraft.yaml file:

name: my-service
version: '1.0'
summary: My backend service
description: |
  A service that runs in the background and automatically restarts if it crashes.

grade: stable
confinement: strict
base: core20  # Use an appropriate base

apps:
  service:
    command: bin/my-service-executable
    daemon: simple
    restart-condition: always
    restart-delay: 10s
    plugs: [network, network-bind]  # Add appropriate plugs

parts:
  my-service:
    source: .
    plugin: go  # Use the appropriate plugin for your application
    # Additional build instructions

Key service configurations:

  • daemon: simple - Indicates this is a background service
  • restart-condition: always - Makes the service restart automatically if it exits
  • restart-delay: 10s - Waits 10 seconds before restarting the crashed service

2. Build and install your Snap

# Build the snap
snapcraft

# Install it locally
sudo snap install my-service_1.0_amd64.snap --dangerous

Managing Your Snap Service

Once your Snap is installed, you can manage the service with these commands:

Start the service

sudo snap start my-service.service

Stop the service

sudo snap stop my-service.service

Restart the service

sudo snap restart my-service.service

Check service status

snap services my-service

Enable auto-start at boot

sudo snap enable my-service.service

Disable auto-start at boot

sudo snap disable my-service.service

Viewing Service Logs

You can check the service logs with:

snap logs my-service

For continuous monitoring:

snap logs -f my-service

Important Notes

  1. The service will automatically restart based on the restart-condition you specified in the snapcraft.yaml file.

  2. The daemon setting has several options:

    • simple: Standard service that starts and continues running
    • oneshot: Runs once and exits
    • forking: Service forks into background
    • notify: Service sends a notification when ready
  3. Make sure to include the appropriate plugs in your snapcraft.yaml to give your service the permissions it needs.

  4. The service name in commands will be <snap-name>.<app-name> - in this example it's my-service.service.

This approach ensures your service is properly managed by snapd, which will monitor and automatically restart it according to your specifications.

Good question — let’s break it down clearly:

snap vs. systemctl

1. What is systemctl (systemd)?

  • systemctl is a command to manage services on systemd, which is the main init system and service manager on Ubuntu (and most Linuxes now).
  • It's native, lightweight, fast.
  • It manages everything that starts/stops/runs on the system — including:
    • Your own custom programs
    • Built-in services like networking, cron, etc.
  • You write plain .service files to control things.

✅ Pros:

  • Full control: you can configure memory limits, restart policies, dependencies, user permissions, everything.
  • Integrated into the OS.
  • Fast, minimal overhead.

❗ Cons:

  • You have to manually create and maintain service files.
  • You are responsible for setting permissions, updates, security.

2. What is Snap?

  • Snap is a packaging and deployment system developed by Canonical (Ubuntu company).
  • Snap can package an app with all its dependencies bundled.
  • Snap also allows apps to define snap services (managed through Snap, not native systemd services).
  • Snap services are defined inside the app's snap package (snapcraft.yaml) — not manually by you.

✅ Pros:

  • Super easy to install apps (snap install xyz) — no dependency hell.
  • Apps are automatically updated and sandboxed for security.
  • Good if you're building apps you want to distribute easily across many distros.

❗ Cons:

  • Snap apps run in a sandbox → sometimes can't easily access the real system (files, network).
  • Snap startup is slower (because of the containerization overhead).
  • Snap services are a bit harder to tweak compared to systemd services.
  • Not all apps are good fits for Snap — especially small, fast backend tools.

So, which is better?

Situation Best choice
You are running a local program, want full control over how it runs and restarts systemctl (systemd)
You are building an app to distribute to others, across multiple Linux versions Snap
You want zero-maintenance auto-updating apps (like a user, not a developer) Snap
You need high performance, low latency backend service systemctl

Very short answer:

  • If you're running your own program, use systemd with systemctl. It’s cleaner, faster, more powerful.
  • If you're packaging apps for others, Snap makes sense.

Comments |0|

Legend *) Required fields are marked
**) You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Category: cloud