简体   繁体   English

如何检查特定服务是否在 Ubuntu 上运行

[英]How to check if a particular service is running on Ubuntu

I do not know the service's name, but would like to stop the service by checking its status.我不知道该服务的名称,但想通过检查其状态来停止该服务。

For example, if I want to check if the PostgreSQL service is running or not, but I don't know the service's name, then how could I check its status?例如,如果我想检查PostgreSQL服务是否正在运行,但我不知道该服务的名称,那么我如何检查它的状态?

I know the command to check the status if the service name is known.如果服务名称已知,我知道检查状态的命令。

I don't have an Ubuntu box, but on Red Hat Linux you can see all running services by running the following command:我没有 Ubuntu 机器,但在Red Hat Linux 上,您可以通过运行以下命令查看所有正在运行的服务:

service --status-all

On the list the + indicates the service is running, - indicates service is not running, ?在列表中, +表示服务正在运行, -表示服务未运行, ? indicates the service state cannot be determined.表示无法确定服务状态。

For Ubuntu (checked with 12.04)对于 Ubuntu(使用 12.04 检查)

You can get list of all services and select by color one of them with 'grep':您可以获得所有服务的列表,并使用“grep”按颜色选择其中之一:

sudo service --status-all | grep postgres

Or you may use another way if you know correct name of service:或者,如果您知道正确的服务名称,您可以使用另一种方式:

sudo service postgresql status

Maybe what you want is the ps command;也许你想要的是 ps 命令;

ps -ef

will show you all processes running.将显示所有正在运行的进程。 Then if you have an idea of what you're looking for use grep to filter;然后,如果您知道要查找的内容,请使用 grep 进行过滤;

ps -ef | grep postgres

There is a simple way to verify if a service is running有一种简单的方法可以验证服务是否正在运行

systemctl status service_name

Try PostgreSQL:试试 PostgreSQL:

systemctl status postgresql

If you run the following command you will get a list of services:如果您运行以下命令,您将获得服务列表:

sudo service --status-all

To get a list of upstart jobs run this command:要获取新贵工作的列表,请运行以下命令:

sudo initctl list

You can use the below command to check the list of all services.您可以使用以下命令检查所有服务的列表。

ps aux 

To check your own service:要检查您自己的服务:

ps aux | grep postgres

the best way is using of nmap tool in terminal.最好的方法是在终端中使用nmap工具。 nmap is an useful tool that analyse an up system, using it's IP Address , then show all actived network services . nmap 是一个有用的工具,可以使用它的IP Address来分析系统,然后显示所有活动的网络services

open terminal and use of this example :打开terminal并使用此示例:

~$ nmap 192.168.1.3/24

Starting Nmap 5.21 ( http://nmap.org ) at 2016-05-16 22:49 IRDT
Nmap scan report for 192.168.1.3
Host is up (0.00020s latency).
Not shown: 994 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
23/tcp   open  telnet
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
3389/tcp open  ms-term-serv
3689/tcp open  rendezvous

run跑步

ps -ef | ps -ef | grep name-related-to-process grep名称相关的进程

above command will give all the details like pid, start time about the process.上面的命令将提供所有详细信息,例如 pid、有关进程的启动时间。

like if you want all java realted process give java or if you have name of process place the name就像如果你想要所有的 java realted 进程给 java 或者如果你有进程的名称放置名称

To check the status of a service on linux operating system :要在 linux 操作系统上检查服务的状态:

//in case of super user(admin) requires    
sudo service {service_name} status 
// in case of normal user
service {service_name} status 

To stop or start service停止或启动服务

// in case of admin requires
sudo service {service_name} start/stop
// in case of normal user
service {service_name} start/stop 

To get the list of all services along with PID :要获取所有服务的列表以及 PID :

sudo service --status-all

You can use systemctl instead of directly calling service :您可以使用 systemctl 而不是直接调用 service :

systemctl status/start/stop {service_name}

Dirty way to find running services.查找正在运行的服务的肮脏方式。 (sometime it is not accurate because some custom script doesn't have |status| option) (有时它不准确,因为某些自定义脚本没有 |status| 选项)

[root@server ~]# for qw in `ls /etc/init.d/*`; do  $qw status | grep -i running; done
auditd (pid  1089) is running...
crond (pid  1296) is running...
fail2ban-server (pid  1309) is running...
httpd (pid  7895) is running...
messagebus (pid  1145) is running...
mysqld (pid  1994) is running...
master (pid  1272) is running...
radiusd (pid  1712) is running...
redis-server (pid  1133) is running...
rsyslogd (pid  1109) is running...
openssh-daemon (pid  7040) is running...

For centos, below command worked for me (:对于centos,以下命令对我有用(:

locate postgres | grep service

Output:输出:

/usr/lib/firewalld/services/postgresql.xml /usr/lib/firewalld/services/postgresql.xml

/usr/lib/systemd/system/ postgresql-9.3.service /usr/lib/systemd/system/ postgresql-9.3.service

sudo systemctl status postgresql-9.3.service

for Centos 6.10 : /sbin/service serviceNAME status Centos 6.10 : /sbin/service serviceNAME status

for Centos 7.6 and ubuntu 18.04: systemctl status NAME.service Centos 7.6 和 ubuntu 18.04: systemctl status NAME.service

works for all of them: service --status-all适用于所有人: service --status-all

Based on this answer on a similar topic https://askubuntu.com/a/58406基于这个关于类似主题的答案https://askubuntu.com/a/58406
I prefer: /etc/init.d/postgres status我更喜欢: /etc/init.d/postgres status

if you are looking particularly for postgres there is a specific command called "pgrep" you can find the usage in the below mentioned article https://mydbanotebook.org/post/troubleshooting-01/如果您特别寻找 postgres,则有一个名为“pgrep”的特定命令,您可以在下面提到的文章https://mydbanotebook.org/post/troubleshooting-01/ 中找到用法

this article provides the following details: check if postgres server is running where does postgres store all the server config how to start/stop postgres本文提供了以下详细信息: 检查 postgres 服务器是否正在运行 postgres 在哪里存储所有服务器配置 如何启动/停止 postgres

hope this is helpful希望这是有帮助的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM