{"id":2007,"date":"2024-02-25T23:50:23","date_gmt":"2024-02-25T22:50:23","guid":{"rendered":"https:\/\/robermb.com\/blog\/?p=2007"},"modified":"2024-02-26T08:41:49","modified_gmt":"2024-02-26T07:41:49","slug":"systemctl-linux-services-cheatsheet","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/systemctl-linux-services-cheatsheet\/","title":{"rendered":"systemctl: Linux services cheatsheet"},"content":{"rendered":"\n<p>A list of useful commands to manage linux services with systemctl.<\/p>\n\n\n\n<ul>\n<li>Provide detailed information about the specified service, including its current state:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl status your-service-name<\/code><\/pre>\n\n\n\n<ul>\n<li>To start \/ stop a service:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl start your-service-name\n$ systemctl stop your-service-name<\/code><\/pre>\n\n\n\n<ul>\n<li>To enable a service to start automatically at boot time:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl enable your-service-name<\/code><\/pre>\n\n\n\n<ul>\n<li>To disable a service from starting automatically at boot time:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl disable your-service-name<\/code><\/pre>\n\n\n\n<ul>\n<li>List all enabled (i.e., set to start at boot) service units on a system:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl list-unit-files --type=service --state=enabled\n\nUNIT FILE              STATE  \nautovt@.service        enabled\ngetty@.service         enabled\nkdump.service          enabled\nnis-domainname.service enabled\nsshd.service           enabled\n\n5 unit files listed.<\/code><\/pre>\n\n\n\n<ul>\n<li>Lists service units that are both enabled and currently running on the system:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl list-units --type=service --state=enabled,running\n\nUNIT                     LOAD   ACTIVE SUB     DESCRIPTION               \ndbus.service             loaded active running D-Bus System Message Bus  \nsshd.service             loaded active running OpenSSH server daemon     \nsystemd-journald.service loaded active running Journal Service           \nsystemd-udevd.service    loaded active running udev Kernel Device Manager\n\nLOAD   = Reflects whether the unit definition was properly loaded.\nACTIVE = The high-level unit activation state, i.e. generalization of SUB.\nSUB    = The low-level unit activation state, values depend on unit type.\n\n4 loaded units listed. Pass --all to see loaded but inactive units, too.\nTo show all installed unit files use 'systemctl list-unit-files'.<\/code><\/pre>\n\n\n\n<ul>\n<li>Provides a list of names of all failed systemd units on the system:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl --quiet --no-pager --failed --all --full --no-legend | awk '{print $1}'<\/code><\/pre>\n\n\n\n<ul>\n<li>Provides detailed information about the configuration and status of <code>a<\/code> service:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl show sshd.service\n\nType=notify\nRestart=on-failure\nNotifyAccess=main\nRestartUSec=42s\nTimeoutStartUSec=1min 30s\nTimeoutStopUSec=1min 30s\n...<\/code><\/pre>\n\n\n\n<ul>\n<li>Check Status of Multiple Services:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl status sshd.service httpd.service<\/code><\/pre>\n\n\n\n<ul>\n<li>List Failed Units:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl --quiet --no-pager --failed<\/code><\/pre>\n\n\n\n<ul>\n<li>Check Dependencies of a Service:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl list-dependencies sshd.service<\/code><\/pre>\n\n\n\n<ul>\n<li>Restart All Failed Services:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl list-units --state=failed --no-pager --quiet | awk '{print $1}' | xargs systemctl restart<\/code><\/pre>\n\n\n\n<ul>\n<li>Show All Units Sorted by Startup Time:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl list-units --all --no-pager --no-legend --sort=start<\/code><\/pre>\n\n\n\n<ul>\n<li>Monitor Journal in Real-time for a Service:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ journalctl -fu sshd.service<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Service file configuration example<\/h3>\n\n\n\n<p>Basic example of a systemd service file that is located in <code>\/etc\/systemd\/system\/<\/code> with the name &#8216;<code>example-service.service<\/code>&#8216;. This example is for a fictional service called &#8216;<code>example-service<\/code>&#8216;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \/etc\/systemd\/system\/example-service.service\n\n&#91;Unit]\nDescription=Example Service\nAfter=network.target\n\n&#91;Service]\nExecStart=\/usr\/bin\/example-service\nRestart=always\nUser=nobody\n\n&#91;Install]\nWantedBy=default.target<\/code><\/pre>\n\n\n\n<ul>\n<li><code>Description<\/code>: A human-readable description of the service.<\/li>\n\n\n\n<li><code>After<\/code>: Specifies that the service should start after <code>network.target<\/code> has been reached.<\/li>\n\n\n\n<li><code>ExecStart<\/code>: The path to the executable or command to start the service.<\/li>\n\n\n\n<li><code>Restart<\/code>: Indicates that the service should always be restarted if it exits.<\/li>\n\n\n\n<li><code>User<\/code>: The user under which the service will run.<\/li>\n\n\n\n<li><code>WantedBy<\/code>: Indicates the default target that should enable the service.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Reload systemd:<\/strong><\/h3>\n\n\n\n<p>After creating or modifying a service file, you need to reload the systemd configuration to recognize the changes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ systemctl daemon-reload<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A list of useful commands to manage linux services with systemctl. Service file configuration example Basic example of a systemd &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/systemctl-linux-services-cheatsheet\/\" class=\"more-link\">More <span class=\"screen-reader-text\">systemctl: Linux services cheatsheet<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1856,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,8],"tags":[35,48,130],"_links":{"self":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2007"}],"collection":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/comments?post=2007"}],"version-history":[{"count":15,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2007\/revisions"}],"predecessor-version":[{"id":2025,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2007\/revisions\/2025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media\/1856"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=2007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=2007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=2007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}