{"id":2048,"date":"2025-04-28T15:25:50","date_gmt":"2025-04-28T07:25:50","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=2048"},"modified":"2025-04-28T17:00:18","modified_gmt":"2025-04-28T09:00:18","slug":"managing-services-with-snap-on-ubuntu","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=2048","title":{"rendered":"Managing Services with Snap on Ubuntu"},"content":{"rendered":"<h1>Managing Services with Snap on Ubuntu<\/h1>\n<p>On Ubuntu, if you want to use <code>snap start<\/code> 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.<\/p>\n<h2>Creating a Snap with Service Capabilities<\/h2>\n<h3>1. Create a snapcraft.yaml file<\/h3>\n<p>The key to making your application run as a service through Snap is defining it properly in your <code>snapcraft.yaml<\/code> file:<\/p>\n<pre><code class=\"language-yaml\">name: my-service\nversion: &#039;1.0&#039;\nsummary: My backend service\ndescription: |\n  A service that runs in the background and automatically restarts if it crashes.\n\ngrade: stable\nconfinement: strict\nbase: core20  # Use an appropriate base\n\napps:\n  service:\n    command: bin\/my-service-executable\n    daemon: simple\n    restart-condition: always\n    restart-delay: 10s\n    plugs: [network, network-bind]  # Add appropriate plugs\n\nparts:\n  my-service:\n    source: .\n    plugin: go  # Use the appropriate plugin for your application\n    # Additional build instructions<\/code><\/pre>\n<p><strong>Key service configurations:<\/strong><\/p>\n<ul>\n<li><code>daemon: simple<\/code> - Indicates this is a background service<\/li>\n<li><code>restart-condition: always<\/code> - Makes the service restart automatically if it exits<\/li>\n<li><code>restart-delay: 10s<\/code> - Waits 10 seconds before restarting the crashed service<\/li>\n<\/ul>\n<h3>2. Build and install your Snap<\/h3>\n<pre><code class=\"language-bash\"># Build the snap\nsnapcraft\n\n# Install it locally\nsudo snap install my-service_1.0_amd64.snap --dangerous<\/code><\/pre>\n<h2>Managing Your Snap Service<\/h2>\n<p>Once your Snap is installed, you can manage the service with these commands:<\/p>\n<h3>Start the service<\/h3>\n<pre><code class=\"language-bash\">sudo snap start my-service.service<\/code><\/pre>\n<h3>Stop the service<\/h3>\n<pre><code class=\"language-bash\">sudo snap stop my-service.service<\/code><\/pre>\n<h3>Restart the service<\/h3>\n<pre><code class=\"language-bash\">sudo snap restart my-service.service<\/code><\/pre>\n<h3>Check service status<\/h3>\n<pre><code class=\"language-bash\">snap services my-service<\/code><\/pre>\n<h3>Enable auto-start at boot<\/h3>\n<pre><code class=\"language-bash\">sudo snap enable my-service.service<\/code><\/pre>\n<h3>Disable auto-start at boot<\/h3>\n<pre><code class=\"language-bash\">sudo snap disable my-service.service<\/code><\/pre>\n<h2>Viewing Service Logs<\/h2>\n<p>You can check the service logs with:<\/p>\n<pre><code class=\"language-bash\">snap logs my-service<\/code><\/pre>\n<p>For continuous monitoring:<\/p>\n<pre><code class=\"language-bash\">snap logs -f my-service<\/code><\/pre>\n<h2>Important Notes<\/h2>\n<ol>\n<li>\n<p>The service will automatically restart based on the <code>restart-condition<\/code> you specified in the snapcraft.yaml file.<\/p>\n<\/li>\n<li>\n<p>The <code>daemon<\/code> setting has several options:<\/p>\n<ul>\n<li><code>simple<\/code>: Standard service that starts and continues running<\/li>\n<li><code>oneshot<\/code>: Runs once and exits<\/li>\n<li><code>forking<\/code>: Service forks into background<\/li>\n<li><code>notify<\/code>: Service sends a notification when ready<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Make sure to include the appropriate <code>plugs<\/code> in your snapcraft.yaml to give your service the permissions it needs.<\/p>\n<\/li>\n<li>\n<p>The service name in commands will be <code>&lt;snap-name&gt;.&lt;app-name&gt;<\/code> - in this example it's <code>my-service.service<\/code>.<\/p>\n<\/li>\n<\/ol>\n<p>This approach ensures your service is properly managed by snapd, which will monitor and automatically restart it according to your specifications.<\/p>\n<p>Good question \u2014 let\u2019s break it down clearly:<\/p>\n<h1>snap vs. systemctl<\/h1>\n<h2>1. What is <code>systemctl<\/code> (systemd)?<\/h2>\n<ul>\n<li><code>systemctl<\/code> is a command to manage <strong>services<\/strong> on <strong>systemd<\/strong>, which is <strong>the main init system and service manager<\/strong> on Ubuntu (and most Linuxes now).<\/li>\n<li>It's native, lightweight, fast.<\/li>\n<li>It manages <strong>everything that starts\/stops\/runs<\/strong> on the system \u2014 including:\n<ul>\n<li>Your own custom programs<\/li>\n<li>Built-in services like networking, cron, etc.<\/li>\n<\/ul>\n<\/li>\n<li>You write <strong>plain <code>.service<\/code> files<\/strong> to control things.<\/li>\n<\/ul>\n<p>\u2705 Pros:<\/p>\n<ul>\n<li>Full control: you can configure memory limits, restart policies, dependencies, user permissions, everything.<\/li>\n<li>Integrated into the OS.<\/li>\n<li>Fast, minimal overhead.<\/li>\n<\/ul>\n<p>\u2757 Cons:<\/p>\n<ul>\n<li>You have to manually create and maintain service files.<\/li>\n<li>You are responsible for setting permissions, updates, security.<\/li>\n<\/ul>\n<hr \/>\n<h2>2. What is Snap?<\/h2>\n<ul>\n<li>Snap is a <strong>packaging and deployment system<\/strong> developed by Canonical (Ubuntu company).<\/li>\n<li>Snap can package an app <strong>with all its dependencies<\/strong> bundled.<\/li>\n<li>Snap also allows apps to define <strong>snap services<\/strong> (managed through Snap, not native systemd services).<\/li>\n<li>Snap services are defined inside the app's snap package (<code>snapcraft.yaml<\/code>) \u2014 not manually by you.<\/li>\n<\/ul>\n<p>\u2705 Pros:<\/p>\n<ul>\n<li>Super easy to install apps (<code>snap install xyz<\/code>) \u2014 no dependency hell.<\/li>\n<li>Apps are automatically updated and sandboxed for security.<\/li>\n<li>Good if you're building apps you want to distribute easily across many distros.<\/li>\n<\/ul>\n<p>\u2757 Cons:<\/p>\n<ul>\n<li>Snap apps run in a sandbox \u2192 sometimes can't easily access the real system (files, network).<\/li>\n<li>Snap startup is slower (because of the containerization overhead).<\/li>\n<li>Snap services are a bit harder to tweak compared to systemd services.<\/li>\n<li>Not all apps are good fits for Snap \u2014 especially small, fast backend tools.<\/li>\n<\/ul>\n<hr \/>\n<h2>So, <strong>which is better<\/strong>?<\/h2>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Situation<\/th>\n<th style=\"text-align: left;\">Best choice<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">You are running a <strong>local program<\/strong>, want <strong>full control<\/strong> over how it runs and restarts<\/td>\n<td style=\"text-align: left;\">\u2794 <code>systemctl<\/code> (systemd)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">You are <strong>building an app to distribute<\/strong> to others, across multiple Linux versions<\/td>\n<td style=\"text-align: left;\">\u2794 <code>Snap<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">You want <strong>zero-maintenance auto-updating apps<\/strong> (like a user, not a developer)<\/td>\n<td style=\"text-align: left;\">\u2794 <code>Snap<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">You need <strong>high performance, low latency<\/strong> backend service<\/td>\n<td style=\"text-align: left;\">\u2794 <code>systemctl<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>Very short answer:<\/h2>\n<ul>\n<li>If you're <strong>running your own program<\/strong>, <strong>use systemd with systemctl<\/strong>. It\u2019s cleaner, faster, more powerful.<\/li>\n<li>If you're <strong>packaging apps for others<\/strong>, Snap makes sense.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=2048\" title=\"Permanent Link to: Managing Services with Snap on Ubuntu\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-2048","post","type-post","status-publish","format-standard","hentry","category-cloud"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2048"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2048"}],"version-history":[{"count":2,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2048\/revisions"}],"predecessor-version":[{"id":2051,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2048\/revisions\/2051"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}