Use systemd to edit Linux service

Tram Ho

Over the last few years, Linux and its distributions have been transformed through the use of systemd as the init system. This article will introduce systemd commands and create a service and run it with systemctl.

Basic Unit Management

Systemd manages the units . Units come in 12 different types, but the most commonly used type is a service.

To manage service in Linux, we use the systemctl tool

Start service

Stop service

Restart service

Reload service

By default, services in linux are not automatically turned on when starting the operating system. To do that, you need to enable the service

Disable service

Getting an Overview of the System State

We can view unit information with systemctl

  • List units files are systemd list “active”

io

  • List of unit files loaded into memory but currently not active

  • List of unit files that have not been loaded into memory

Unit States and Logs

To see the status of the service units you can use the status option of systemctl

To view all unit log entries

If you only want to list the entries of the current boot session, add the -b option

Inspecting Units and Unit Files

Now we know how to adjust the state of the unit by starting or stopping the service through systemctl.

The unit file contains the parameters that systemd uses to manage and run the unit. To see its full information:

To see which units run on nginx boot

show recursive

Finally, to see specific information about the low level of unit settings (the values ​​of the parameters are controlled by systemd).

Modify unit file

We can edit the unit file without having to go into the directory to find it

Append adds an option to the unit file

Directly edit all file contents

Reload systemd again after fixing

Use Systemd to start service at boot

We will try to create a custom service, run it on boot with systemd

The service file will run the following shell script

Copy the script file to the bin directory

Create a unit file (for full options, please refer to https://www.freedesktop.org/software/systemd/man/systemd.service.html )

  • Start & see status

  • Enable service on boot

https://www.linode.com/docs/guides/start-service-at-boot/

https://www.digitalocean.com/community/tutorials/systemd-essentials-working-with-services-units-and-the-journal

Share the news now

Source : Viblo