Controlling Systemd services with Ubuntu systemctl

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Systemd is a powerful and flexible system and service manager for Linux operating systems, specifically targeting the use of init. Systemd replaces sysvinit (the previous default) by introducing more robustness in managing services on Linux based Operating Systems’.

The “create systemd service ubuntu” is a command-line tool that allows users to create, stop, and restart services provided by the Systemd. The “systemctl” is a command-line tool that can be used to control systemd services.

Controlling Systemd services with Ubuntu systemctl

If you don’t know what you’re doing, managing a service or collection of services on a contemporary Linux PC may be a pain. Fortunately, Ubuntu Systemctl has you covered when it comes to managing services on a Linux computer.

In this article, you’ll learn how to use systemctl commands to control Systemd services on an Ubuntu PC.

Ready? It’s time to get your hands dirty!

Prerequisites

This lesson will be a hands-on example, but no special tools are required; as long as you’re running Ubuntu 16 or above, you’ll be OK. The samples in this course are based on Ubuntu 20.04.3 LTS.

What is the Ubuntu Systemctl command?

Before you perform systemctl commands, you need first learn what Systemctl is. Systemctl is a command-line tool for managing and controlling systemd and other system functions.

Systemctl is a control panel or service manager for the systemd init system, which is used to initialize components once the Linux kernel has been started. Systemctl is a toolkit for managing services that includes libraries, daemons, and tools.

To efficiently manage services, be aware of the following service states:

  • Enabled – indicates that a service is set to start automatically when the system powers up.
  • Disabled – indicates that a service is set to not start when the system starts up.
  • Active – signifies that a service is presently operational.
  • Inactive – indicates that a service is not now operating, but that it may start if another program attempts to start it.

All Services are Listed

Now that you know the different states of services, perhaps you want to see a list of all the services on your Ubuntu PC. All Services are Listed provides a handful of information that prepares you for managing services.

To display all the service units (list-units) (active, running, exited, or failed) with the service specified as the unit type (—type=service), open your terminal and enter the command below.

list-units —type=service systemctl

Choose any service from the list that you wish to control later in this lesson and write down the UNIT name (e.g., apache2). To scroll down, use the spacebar, or press the “Q” key to terminate the command.

All Services are Listed (active, running, exited, or failed) All Services are Listed (active, running, exited, or failed)

Related: The Only Linux Shell Scripting Tutorial You’ll Ever Need

Services with a Specific State Listing

Instead of All Services are Listed, perhaps you want to narrow down the list of services. If so, adding the –state parameter will do the trick, as it filters out the services with the specific state you specify.

To display all active services (—type=service —state=active), both running and exited, use one of the commands below.

list-units —type=service systemctl –state=active

Listing Services on "active" State Services are listed in a “active” state.

Similarly, modify the status value from active to either running or exited to display just exited or running services, as indicated in the instructions below:

—type=service —state=running systemctl —type=service —state=exited systemctl

Listing Services on "running" State Listing Services are now in a “running” state.

Listing Services on "stopped" State Listing Services are now in a “stopped” state.

Enabled Services are listed here.

By pipelining the grep command, you may list enabled and disabled services in addition to active, running, and exiting services.

To list (list-unit-files) disabled or enabled services (—type=service), use one of the instructions below.

## Displays a list of all enabled services. —type=service | grep enabled systemctl list-unit-files ## Displays a list of all disabled services. list-unit-files —type=service | grep disabled systemctl

Take note of an enabled service, as indicated below, since you’ll need it later to disable one; however, this article utilizes the acpid.service for demonstration purposes. The Advanced Configuration and Power Interface (ACPI) events are supported by the acpid daemon, which is an extendable daemon.

Enabled Services are listed here. Enabled Services are listed here.

Property Listing Services

Aside from listing services, you may wish to look at the properties of a service. Understanding service attributes is useful for debugging, process parameters, and restart behavior.

To see the properties of a service (acpid.service) in a key=value format, use the command below.

display acpid.service systemctl

Service Properties are being shown. Service Properties are being shown.

If you simply want to check for a certain service property, use the -p option along with the property name.

To see the MainPID property of a service (acpid.service), use the command below.

display acpid.service systemctl -p MainPID

Specifying a Service Property Specifying a Service Property

How to Kill a Process in Linux Using ps, pgrep, pkill, and Other Commands

Taking Care of a Specific Service

Now that you’ve mastered the fundamentals of listing services, you can use the systemctl stop and start commands to handle a single (single) service.

Apache service (apache2) is used for the following demos, but you may freely manage the service you noted in the “All Services are Listed” section. But before you stop or start a service, you first need to verify the service’s status.

To obtain specific information about the apache2 service, such as its status, use the command below on a terminal.

The apache2 service is alive and operating, as seen below.

Viewing the status of the apache2 service Viewing the status of the apache2 service

Because you are not modifying the service state, the sudo password is not required while examining service status. If you want to start, halt, or restart a service, you must use sudo and input the sudo password.

Putting a Service on Hold

Perhaps you wish to terminate a service that has been left operating in the background. The systemctl stop command is what you’ll need in such instance. To stop the apache2 service, use the systemctl command below.

sudo systemctl apache2 shutdown

If you want to stop several services at once, use sudo systemctl stop sshd apache2 instead of sudo systemctl stop sshd apache2. You may use this command to halt both the sshd and apache2 services at the same time.

Run the systemctl status command again to see whether the apache2 service is still running.

The status of the apache2 service has changed to inactive (dead), indicating that the service has ended.

Checking to see whether the apache2 service is stopped Checking to see whether the apache2 service is stopped

Starting a Business

So, what do you do if you realize a service isn’t working at all? The systemctl start command, which is equivalent to halting service, is used. To start the apache2 service, use the following command.

start apache2 sudo systemctl

You can start several services in a single line of command, just as you can stop multiple services. Substitute the start command for the stop command, as follows: start sshd apache2 sudo systemctl

Run systemctl status one more time to make sure the apache2 service is up and functioning.

If you observe the active (running) status, as shown below, you’re good to go since the service is back up and running.

Apache2 service is now running. Apache2 service is now running.

If you’d rather start the service immediately away rather than stopping it beforehand. If that’s the case, use the systemctl restart command instead: sudo systemctl apache2 restart

Activating or Deactivating a Service

Perhaps you want to activate or block a certain service from starting when the system boots up. If that’s the case, using the systemctl enable or disable command is the best bet.

The enable subcommand allows you to customize your system’s default startup settings, while the disable subcommand prohibits a service from starting when the system boots up.

Run the systemctl command below to disable the service (acpid.service) you noted in the “Enabled Services are listed here.” section. Enter your sudo password to authorize running the command.

deactivate acpid.service with sudo systemctl

Enabling or Disabling a Service Enabling or Disabling a Service

Now since acpid.service supports OS-directed configuration and Power Management (OSPM); you need to re-enable it. The command to enable service is similar to Enabling or Disabling a Service.

To enable acpid.service, use the systemctl command below. This command restores your system’s default starting settings for the service.

enable acpid.service with sudo systemctl

Getting a Service to Work Getting a Service to Work

Conclusion

You’ve learned how to use Ubuntu systemctl commands to rapidly stop, start, or restart services in this lesson. You’ve also discovered that the systemctl command allows you to handle many services at once.

Why not automate the management of services in your Ubuntu system with your newfound knowledge?

The “systemctl service” is a command-line tool that allows users to control Systemd services. It can be used to start, stop, restart and reload the service.

Related Tags

  • ubuntu systemctl list services
  • systemctl ubuntu
  • systemctl enable service
  • ubuntu systemctl vs service
  • ubuntu systemctl not found