How to check if a service is active in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check the status of services running on a Linux system. We will explore different methods, starting with the modern systemctl status command, understanding its output and significance even in environments where direct execution is limited.

Following that, you will learn how to use the older but still relevant service command to confirm service status and finally, verify the running process associated with a service using the ps aux command. This hands-on experience will equip you with essential skills for monitoring and managing services in Linux.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") subgraph Lab Skills linux/echo -.-> lab-558679{{"How to check if a service is active in Linux"}} linux/grep -.-> lab-558679{{"How to check if a service is active in Linux"}} linux/ps -.-> lab-558679{{"How to check if a service is active in Linux"}} linux/service -.-> lab-558679{{"How to check if a service is active in Linux"}} end

Check service status using systemctl status

In this step, you'll learn how to check the status of system services using the systemctl command. Services are background programs that run on your system, like web servers, database servers, or networking daemons.

While you can't directly use systemctl in this specific LabEx environment (due to it running in a Docker container), understanding how it works is crucial for managing services on a real Linux system. We'll simulate the concept and show you the typical command usage.

On a standard Linux system using systemd (which is common in modern distributions like Ubuntu), you would use systemctl status followed by the service name to check if a service is running, active, or failed.

For example, to check the status of the ssh service (which handles secure remote logins), you would typically run:

systemctl status ssh

The output would look something like this (the exact details might vary):

โ— ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since ...
       Docs: man:sshd(8)
             man:ssh(1)
   Main PID: ... (sshd)
      Tasks: ... (limit: ...)
     Memory: ...
        CPU: ...
     CGroup: /system.slice/ssh.service
             โ””โ”€... /usr/sbin/sshd -D

...

Key things to look for in the output:

  • Loaded: Shows if the service configuration was loaded correctly.
  • Active: Indicates the current state of the service (e.g., active (running), inactive (dead), failed).
  • Main PID: The process ID of the main process for the service.

Even though you can't run systemctl here, it's important to know this command for managing services on other Linux systems.

To simulate checking a service status and practice command execution, let's use echo again to print a message indicating you've learned about systemctl status.

Type the following command in your terminal:

echo "Learned about systemctl status"

Press Enter.

This command simply confirms you've read and understood the concept of using systemctl status.

Click Continue to proceed.

Confirm service with service command output

In this step, you'll learn about another command used to manage services, the service command. This command is often used on systems that don't use systemd or for compatibility with older init systems.

The service command provides a simpler interface for starting, stopping, restarting, and checking the status of services.

The basic syntax to check the status of a service using the service command is:

service <service-name> status

Similar to systemctl, you would replace <service-name> with the actual name of the service you want to check.

For example, on a system where the apache2 web server is installed and managed by the service command, you would check its status like this:

service apache2 status

The output would typically indicate whether the service is running or not.

Again, while you cannot execute this command directly in this LabEx environment due to its containerized nature, understanding the service command is valuable for working with different Linux systems.

To confirm you've learned about the service command, use the echo command to print a confirmation message.

Type the following command in your terminal:

echo "Understood service command"

Press Enter.

This action serves as a marker that you have completed this step and learned about the service command.

Click Continue to move to the next step.

Verify running service process with ps aux

In this step, you'll learn how to verify if a service is running by looking for its associated processes using the ps aux command. This is a fundamental way to see what programs are currently executing on your system.

The ps command reports a snapshot of the current processes. The aux options are commonly used to display processes for all users (a), including those not attached to a terminal (x), and to show detailed information in a user-friendly format (u).

When a service is running, it typically has one or more processes associated with it. You can use ps aux and pipe its output to grep to filter for processes related to a specific service.

For example, to see if there are any processes related to the ssh service, you would use:

ps aux | grep ssh

Let's try this in our LabEx environment. We know the terminal itself is a running process. Let's use ps aux to find the process for our zsh shell.

Type the following command in your terminal:

ps aux | grep zsh

Press Enter.

You should see output similar to this:

labex       ...  ...  ...  ... ?        Ss   ... ...:.. /usr/bin/zsh
labex       ...  ...  ...  ... ?        S+   ... ...:.. grep --color=auto zsh

The first line shows the zsh process (your terminal shell), and the second line shows the grep command you just ran (which is also a process!).

This method (ps aux | grep <service-name>) is a powerful way to confirm if a service is truly running by checking for its active processes, especially when systemctl or service commands might not be available or provide enough detail.

To complete this step, use echo to confirm you've practiced using ps aux with grep.

Type the following command:

echo "Practiced ps aux with grep"

Press Enter.

Click Continue to finish the lab.

Summary

In this lab, you learned how to check the status of services in Linux using various methods. You were introduced to the systemctl status command, which is the standard way to check service status on modern Linux systems using systemd, although direct execution was simulated due to the lab environment. You also learned how to confirm service status using the older service command and how to verify a running service process by examining the process list with ps aux. These steps provide a comprehensive approach to determining if a specific service is active and running on a Linux system.