Sunday, June 29, 2014

How to Manage Services with systemd

For all of you Red Hat enthusiasts out there, you are probably aware that with the releases of CentOS 7 and the long anticipated RHEL 7 comes the "new init", SYSTEMD. Many of Linux distros, including Fedora since release 15 (I believe), have already made the switch or are in process of switching. Most of my experience lies with the Red Hat distros so Ill be demoing on those. There are many benefits of switching to systemd such as tracking processes using cgroups, support for snapshots and state restore (much like the what the virtualization world uses) and optimized parallelization. Many more benefits that I wont get into for this post. For more of an overview or information visit the Fedora Project's Wiki on Systemd. Ill have many more systemd posts to come but for now lets just focus on managing services. For this tutorial we will demonstrate on the httpd service due to its popularity in the Linux World. If following along please ensure that httpd is installed. Let's begin!

One of the biggest and most flattering differences for managing services in systemd, atleast for me, is the fact that you only need to use one command line tool, systemctl(Be sure to check out the man page), whereas the traditional init system needs both chkconfig and service tools to manage services.

To see the list of available services on your machine, run the following command:

  [root@localhost ~]# systemctl list-unit-files --type=service

         ***Additionally if you want to search for a specific service, feel free to grep for it.


In our case the httpd service is "httpd.service". To check to see if the service is running, use the following command:

      [root@localhost ~]# systemctl status httpd.service
   httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
       Active: inactive (dead)


Note how descriptive the output is. The output gives a short couple of words on what the service is, if its loaded, if enabled/disabled for boot time and its state which in this case is not running. Note the output of the status of the httpd service after issuing start, stop and restart below.

To start the httpd service issue the start command:

    [root@localhost ~]# systemctl start httpd.service
   [root@localhost ~]# systemctl status httpd.service
   httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
       Active: active (running) since Fri 2014-06-27 16:18:19 EDT; 6s ago
    Main PID: 2049 (httpd)
       Status: "Processing requests..."
       CGroup: /system.slice/httpd.service
           ├─2049 /usr/sbin/httpd -DFOREGROUND
           ├─2050 /usr/sbin/httpd -DFOREGROUND
           ├─2051 /usr/sbin/httpd -DFOREGROUND
           ├─2052 /usr/sbin/httpd -DFOREGROUND
           ├─2053 /usr/sbin/httpd -DFOREGROUND
           └─2054 /usr/sbin/httpd -DFOREGROUND

    Jun 27 16:18:19 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
    Hint: Some lines were ellipsized, use -l to show in full.


To stop the service run:

    [root@localhost ~]# systemctl stop httpd.service
    [root@localhost ~]# systemctl status httpd.service
    httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
       Active: inactive (dead)

    Jun 27 16:18:19 localhost.localdomain systemd[1]: Starting The Apache HTTP Se...
    Jun 27 16:18:19 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
    Jun 27 16:19:02 localhost.localdomain systemd[1]: Stopping The Apache HTTP Se...
    Jun 27 16:19:03 localhost.localdomain systemd[1]: Stopped The Apache HTTP Ser...
    Hint: Some lines were ellipsized, use -l to show in full.



To restart httpd, run below command:

    [root@localhost ~]# systemctl restart httpd.service
    [root@localhost ~]# systemctl status httpd.service
    httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
       Active: active (running) since Fri 2014-06-27 16:19:31 EDT; 2s ago
    Main PID: 2070 (httpd)
       Status: "Processing requests..."
       CGroup: /system.slice/httpd.service
           ├─2070 /usr/sbin/httpd -DFOREGROUND
           ├─2071 /usr/sbin/httpd -DFOREGROUND
           ├─2072 /usr/sbin/httpd -DFOREGROUND
           ├─2073 /usr/sbin/httpd -DFOREGROUND
           ├─2074 /usr/sbin/httpd -DFOREGROUND
           └─2075 /usr/sbin/httpd -DFOREGROUND

    Jun 27 16:19:31 localhost.localdomain systemd[1]: Started The Apache HTTP Ser...
    Hint: Some lines were ellipsized, use -l to show in full.



Ill be doing a later segment on configuring boot time and runlevels (known as targets in systemd) in the near future but for the sake of this post let's take a look at enabling and disabling the service at Boot Time. To enable the httpd service to run at boot time, do the following:

    [root@localhost ~]# systemctl enable httpd.service 
    ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

Note the service link to "multi-user.target.wants".


To remove the httpd service from running at boot time:

    [root@localhost ~]# systemctl disable httpd.service 
    rm '/etc/systemd/system/multi-user.target.wants/httpd.service'


Note the service removal of the link that was created when the service was enabled.


This was just a very short introduction and overview into managing services in systemd using systemctl. There are several other things that you can do with systemctl such as kill processes and get a ton of information on them. Feel free to read its man page and as I mentioned above, Fedora's wiki page on systemd is a very good piece on using systemd. Please keep your eyes open as I will be posting a series of demos and discussions on systemd in the near future. Thanks for reading.

No comments:

Post a Comment