Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if a particular service is running on Ubuntu?
We know that we can make use of the top command to print all the processes that are running in the background. Though the top command is used to print the list of processes or threads that are currently managed by the Linux kernel, it is still not a convenient way to check if a particular service is running in the background or not.
In order to understand how to check whether a particular service is running or not, we first must understand what a service actually means in Linux. A service is basically a process or group of processes that is running continuously in the background, also known as daemons.
Now we know what a service is, next is to get an idea about the different commands that we will make use of to check whether a particular service is running or not.
Using the ps Command
The first command is the ps command of the Linux operating system. The ps command is short for "process status" and is used to list the currently running processes on your Linux machine along with their PIDs and different options.
Syntax
ps [options]
Let's make use of two examples where first we will print all the processes that are running on your Ubuntu machine, and next we will print whether a specific service is running or not.
List All Running Processes
ps -ef
UID PID PPID C STIME TTY TIME CMD root 1 0 0 Jun25 ? 00:00:02 /sbin/init root 2 0 0 Jun25 ? 00:00:00 [kthreadd] root 3 2 0 Jun25 ? 00:00:00 [rcu_gp] root 4 2 0 Jun25 ? 00:00:00 [rcu_par_gp]
The output of the above command is huge so I printed only a chunk of it, and it should be noted that the output can vary based on your system.
Check Specific Service
ps -ef | grep ssh
The above command will tell us whether the SSH service is running on your machine or not.
root 1234 1 0 10:30 ? 00:00:00 /usr/sbin/sshd -D user 5678 5432 0 11:45 pts/0 00:00:00 grep ssh
Only if you see the CMD column of the above output filled with the actual service command (like /usr/sbin/sshd), then you can confirm that the service is actually running. The grep process itself will also appear in the output.
Using systemctl Command
Ubuntu uses systemd as its service manager. The most reliable way to check service status is using the systemctl command:
systemctl status service_name
For example, to check if Apache web server is running:
systemctl status apache2
? apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-06-26 10:30:15 UTC; 2h 15min ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 1234 (apache2)
Tasks: 55 (limit: 4915)
Memory: 15.2M
Alternative ps Command Options
Another way to check whether a service is running is to make use of the aux options in place of the -ef flag in the above commands.
ps aux | grep service_name
root 1234 0.0 0.1 65508 2048 ? Ss 10:30 0:00 /usr/sbin/sshd -D user 5678 0.0 0.0 14428 928 pts/0 S+ 11:45 0:00 grep sshd
Quick Service Check Commands
Here are some quick commands to check common services:
systemctl is-active apache2− Returns "active" or "inactive"systemctl is-enabled nginx− Shows if service starts at bootpgrep service_name− Returns PID if service is runningservice --status-all− Lists all services and their status
Conclusion
The most reliable method to check if a service is running on Ubuntu is using systemctl status service_name. While the ps command can also identify running processes, systemctl provides comprehensive service information including status, configuration, and logs. These tools ensure effective service management on Ubuntu systems.
