检查bash中是否存在服务(CentOS和Ubuntu)

时间:2014-06-25 00:04:44

标签: linux bash ubuntu service centos

bash 中检查服务是否已安装的最佳方法是什么?它应该适用于Red Hat(CentOS)和Ubuntu?

思维:

service="mysqld"
if [ -f "/etc/init.d/$service" ]; then
    # mysqld service exists
fi

也可以使用service命令并检查返回码。

service mysqld status
if [ $? = 0 ]; then
    # mysqld service exists
fi

什么是最佳解决方案?

9 个答案:

答案 0 :(得分:12)

Rustam Mamat因此得到了赞誉:

如果列出所有服务,您可以查看结果以查看其中的内容。 E.g:

# Restart apache2 service, if it exists.    
if service --status-all | grep -Fq 'apache2'; then    
  sudo service apache2 restart    
fi

答案 1 :(得分:5)

在SystemD系统上:

serviceName="Name of your service"

if systemctl --all --type service | grep -q "$serviceName";then
    echo "$serviceName exists."
else
    echo "$serviceName does NOT exist."
fi

在新贵系统上:

serviceName="Name of your service"

if initctl list | grep -q "$serviceName";then
    echo "$serviceName exists."
else
    echo "$serviceName does NOT exist."
fi

在SysV(系统V)系统上:

serviceName="Name of your service"

if service --status-all | grep -q "$serviceName";then
    echo "$serviceName exists."
else
    echo "$serviceName does NOT exist."
fi

答案 2 :(得分:4)

在 systemd(尤其是 Debian)中,使用这里的各种答案似乎无法正常工作。对于某些服务,例如 pure-ftpd,如果它处于禁用模式,则在触发此命令时它不会显示在服务列表中:

systemctl --all --type service

当您再次启动 pure-ftpdsystemctl start pure-ftpd 时,列表将再次出现。因此,使用 systemctl --all --type service 列出服务不适用于所有服务。查看at this了解更多信息。

因此,这是迄今为止检查服务是否存在的最佳代码(从@jehon 的答案改进)(即使它的状态为非活动、死机或任何状态):

#!/bin/bash
is_service_exists() {
    local x=$1
    if systemctl status "${x}" 2> /dev/null | grep -Fq "Active:"; then
            return 0
    else
            return 1
    fi
}


if is_service_exists 'pure-ftpd'; then

        echo "Service found!"
else
        echo "Service not found!"
fi

说明:

如果 systemctl status 找到一个服务,它必须有一个文本“Active:”,我们使用 grep 进行过滤,它会返回 0。如果没有“Active:”文本,它会返回 1。

如果 systemctl status 没有找到“Active:”文本,它会打印出一个标准错误。因此,我将重定向 2> /dev/null 用于重定向标准错误。例如,如果您正在寻找不存在的服务,如果您不放置该错误重定向,您将收到此错误消息:

Unit pure-ftpdd.service could not be found.

如果您正在编写脚本,我们不希望出现上述标准错误消息

编辑:

另一种方法是列出@Anthony Rutledge 为 Debian 系统指出的能够检测禁用服务的单元文件:

systemctl list-unit-files --type service | grep -F "pure-ftpd"

但使用此方法并不总是适用于较旧的系统,因为使用此命令可能无法检测到某些单元文件,如 here 中所述。此外,如果您有需要过滤的大型单元文件,则使用此方法会较慢(正如@ygoe 所评论的关于小型计算机上的重负载)。

答案 3 :(得分:3)

以上回答中的service --status-all的问题在于,似乎对每个服务执行ping操作,这可能会很长。

那又怎么样:

systemctl list-units --full -all | grep -Fq "$SERVICENAME.service"

这是bash完成中使用的(在文件/ usr / share / bash-completion / bash_completion中,查找_services):

COMPREPLY+=( $( systemctl list-units --full --all 2>/dev/null | \
   awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' ) )

希望能提供帮助。

答案 4 :(得分:2)

为了建立Joel B的答案,这里它是一个函数(添加了一点灵活性。注意完全没有参数检查;如果你没有传入2个参数,这将会中断):

#!/bin/sh

serviceCommand() {
  if sudo service --status-all | grep -Fq ${1}; then
    sudo service ${1} ${2}
  fi
}

serviceCommand apache2 status

答案 5 :(得分:2)

#!/bin/sh

service=mysql
status=$(/etc/init.d/mysql status)
print "$status"
#echo $status > /var/log/mysql_status_log

答案 6 :(得分:0)

阅读一些系统手册页后...

https://www.freedesktop.org/software/systemd/man/systemd.unit.html

...和systemd.services(5)....

...还有一篇不错的小文章...

https://www.linux.com/learn/understanding-and-using-systemd

我相信这可能是一个答案。

systemctl list-unit-files -type service

通过管道awk {'print $ 1'}可以获得服务单元的列表。

再次管道以awk独家获得服务名称。用-F将字段分隔符更改为句点。

awk -F。打印{'打印$ 1'}

随着基本解决方案的变化和扩充,您可以通过将for循环与systemctl is-active $service组合在一起来确定系统服务的状态。

答案 7 :(得分:0)

var=$(service --status-all | grep -w "$Service")
if [ "output" != "" ]; then
    #executes if service exists
else
    #executes if service does not exist
fi

$ Service是您想知道是否存在的服务的名称。 var将包含类似

[+]    apache2

如果该服务确实存在

答案 8 :(得分:-1)

试试这个,因为ps命令可以在Ubuntu和RHEL中使用,这应该适用于两个平台。

#!/bin/bash
ps cax | grep mysqld > /dev/null
if [ $? -eq 0 ]; then
  echo "mysqld service exists"
else
  echo "mysqld service not exists"
fi