{"id":72177,"date":"2023-03-23T09:00:59","date_gmt":"2023-03-23T09:00:59","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=72177"},"modified":"2023-03-23T09:00:59","modified_gmt":"2023-03-23T09:00:59","slug":"3-surprising-things-linux-sysadmins-can-do-with-systemd","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=72177","title":{"rendered":"3 surprising things Linux sysadmins can do with systemd"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">3 surprising things Linux sysadmins can do with systemd<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/alansmithee\" class=\"username\">alansmithee<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Thu, 03\/23\/2023 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>When it first started out, there was a lot of press about systemd and its ability to speed up boot time. That feature had a mostly-universal appeal (it&#8217;s less important to those who don&#8217;t reboot), so in many ways, that&#8217;s the reputation it still has today. And while it&#8217;s true that systemd is the thing that launches services in parallel during startup, there&#8217;s a lot more to it than that. Here are three things you may not have realized systemd could do but should be taking advantage. Get more tips from our new downloadable eBook, <strong><a href=\"https:\/\/opensource.com\/downloads\/pragmatic-guide-systemd-linux\" target=\"_blank\" rel=\"noopener\">A pragmatic guide to systemd<\/a><\/strong>.<\/p>\n<h2>1. Simplify Linux ps<\/h2>\n<p>If you&#8217;ve ever used the <code>ps<\/code> or even just the <code>top<\/code> command, then you know that your computer is running hundreds of processes at any given moment. Sometimes, that&#8217;s exactly the kind of information you need in order to understand what your computer, or its users, are up to. Other times, all you really need is a general overview.<\/p>\n<p>The <code>systemd-cgtop<\/code> command provides a simple view of your computer&#8217;s load based on the cgroups (control groups) tasks have been arranged into. <a href=\"https:\/\/www.redhat.com\/sysadmin\/cgroups-part-four?intcmp=7013a000002qLH8AAM\" target=\"_blank\" rel=\"noopener\">Control groups<\/a> are important to modern Linux, and are essentially the support structures underneath containers and Kubernetes (which in turn are why the cloud scales the way it does), but also they&#8217;re useful constructs on your home PC. For instance, from the output of <code>systemd-cgtop<\/code>, you can see the load of your user processes as opposed to system processes:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-text\" data-lang=\"text\">Control Group               Proc+   %CPU   Memory  Input\/s Output\/s\n\/                             183    5.0     1.6G       0B     3.0M\nuser.slice                      4    2.8     1.1G       0B   174.7K\nuser.slice\/user-1000.slice      4    2.8   968.2M       0B   174.7K\nsystem.slice                   65    2.2     1.5G       0B     2.8M<\/code><\/pre>\n<p>You can also view just your userspace processes, or just your userspace processes and kernel threads.<\/p>\n<p>This isn&#8217;t a replacement for <code>top<\/code> or <code>ps<\/code> by any means, but it&#8217;s an additional view into your system from a different and unique angle. And it can be vital when running containers, because containers use cgroups.<\/p>\n<h2>2. Linux cron<\/h2>\n<p><a href=\"https:\/\/opensource.com\/article\/17\/11\/how-use-cron-linux\" target=\"_blank\" rel=\"noopener\">Cron<\/a> is a classic component of Linux. When you want to schedule something to happen on a regular basis, you use cron. It&#8217;s reliable and pretty well integrated into your system.<\/p>\n<p>The problem is, cron doesn&#8217;t understand that some computers get shut down. If you have a cronjob scheduled for midnight, but you turn your computer off at 23:59 every day, then your cronjob never runs. There&#8217;s no facility for cron to detect that there was a missed job overnight.<\/p>\n<p>As an answer to that problem, there&#8217;s the excellent <a href=\"https:\/\/opensource.com\/article\/21\/2\/linux-automation\">anacron<\/a>, but that&#8217;s not quite as integrated as cron. There&#8217;s a lot of setup you have to do to get anacron running.<\/p>\n<p>A second alternative is systemd timers. Like cron, it&#8217;s already built in and ready to go. You have to write a unit file, which is definitely more lines than a one-line crontab entry, but it&#8217;s also pretty simple. For instance, here&#8217;s a unit file to run an imaginary backup script 30 minutes after startup, but only once a day. This ensures that my computer gets backed up, and prevents it from trying to backup more than once daily.<\/p>\n<pre class=\"highlight\">\n<code class=\"language-bash\" data-lang=\"bash\">[Unit]\nDescription=Backup\nRequires=myBackup.service\n\n[Timer]\nOnBootSec=30min\nOnUnitActiveSec=1d\n\n[Install]\nWantedBy=timers.target<\/code><\/pre>\n<p>You can, of course, intervene and prompt a job to run with . Thanks to the <code>OnUnitActiveSec<\/code> directive, systemd doesn&#8217;t attempt to run a job you&#8217;ve manually activated.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">Linux Containers<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/topics\/containers\/whats-a-linux-container?intcmp=70160000000h1s6AAA\">What are Linux containers?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/topics\/containers\/what-is-kubernetes?intcmp=70160000000h1s6AAA\">What is Kubernetes?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/do080-deploying-containerized-applications-technical-overview?intcmp=70160000000h1s6AAA\">Free online course: Deploy containerized applications<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/kubernetes-sysadmin?intcmp=70160000000h1s6AAA\">eBook: A guide to Kubernetes for SREs and sysadmins<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/rh065-running-containers-red-hat-technical-overview?intcmp=70160000000h1s6AAA\">Free online course: Running containers with Red Hat technical overview<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/podman-cheat-sheet?intcmp=70160000000h1s6AAA\">Podman cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/containers?intcmp=70160000000h1s6AAA\">The latest articles on Linux containers<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>3. Run Linux containers<\/h2>\n<p>Containers make starting up a complex service really easy. You can run a <a href=\"https:\/\/opensource.com\/education\/16\/3\/mattermost-open-source-chat\">Mattermost<\/a> or Discourse server in mere minutes. The hard part, in some cases, is managing and monitoring the containers once you have them running. Podman makes it easy to manage them, but what do use to manage Podman? Well, <a href=\"https:\/\/www.redhat.com\/sysadmin\/podman-run-pods-systemd-services?intcmp=7013a000002qLH8AAM\" target=\"_blank\" rel=\"noopener\">you can use systemd<\/a>.<\/p>\n<p>Podman has a built-in command to generate unit files so your containers can be managed and monitored by systemd:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-bash\" data-lang=\"bash\">$ podman generate systemd --new --files --name example_pod<\/code><\/pre>\n<p>All you have to do then is start the service:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-bash\" data-lang=\"bash\">$ systemctl --user start pod-example_pod.service<\/code><\/pre>\n<p>As with any other service on your computer, systemd ensures that your pod runs no matter what. It logs problems, which you can view with <code>journalctl<\/code> along with your other essential logs, and you can monitor its activity within cgroups using <code>systemd-cgtop<\/code>.<\/p>\n<p>It&#8217;s no <a href=\"https:\/\/www.redhat.com\/en\/technologies\/cloud-computing\/openshift\/aws?intcmp=7013a000002qLH8AAM\" target=\"_blank\" rel=\"noopener\">Kubernetes platform<\/a>, but for one or two containers that you just want to have available on a reliable and predictable basis, Podman and systemd are an amazing pair.<\/p>\n<h2>Download the systemd eBook<\/h2>\n<p>There&#8217;s a lot more to systemd, and you can learn the basics, along with lots of useful and pragmatic tips, from author <a href=\"https:\/\/opensource.com\/users\/dboth\" target=\"_blank\" rel=\"noopener\">David Both<\/a> in his new complimentary <strong><a href=\"https:\/\/opensource.com\/downloads\/pragmatic-guide-systemd-linux\" target=\"_blank\" rel=\"noopener\">pragmatic guide to systemd<\/a><\/strong>.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>It&#8217;s not just for making your computer boot faster. Download our new systemd eBook for Linux sysadmins for more tips.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/03\/yearbook-haff-rx-linux-file-lead_0.png\" width=\"520\" height=\"292\" alt=\"\ufeffWhy the operating system matters even more in 2017\" title=\"\ufeffWhy the operating system matters even more in 2017\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Internet Archive Book Images. Modified by Opensource.com. CC BY-SA 4.0<\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/sysadmin\" hreflang=\"en\">Sysadmin<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux\" hreflang=\"en\">Linux<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/containers\" hreflang=\"en\">Containers<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/03\/cc-by-sa--45.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?destination=\/feed&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>3 surprising things Linux sysadmins can do with systemd alansmithee Thu, 03\/23\/2023 &#8211; 03:00 When it first started out, there was a lot of press about systemd and its ability to speed up boot time. That feature had a mostly-universal appeal (it&#8217;s less important to those who don&#8217;t reboot), so in many ways, that&#8217;s the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":72178,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-72177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/72177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=72177"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/72177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/72178"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=72177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=72177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=72177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}