检查服务是否已安装在Linux中

发布于 2025-01-18 17:12:15 字数 720 浏览 3 评论 0原文

我正在尝试获取该服务,无论它是否安装。该脚本在 systemctl 中运行良好,但在未安装 systemctl 的情况下失败。作为替代方案,我使用服务命令进行检查,但这样做我无法运行 grep 命令来 grep 特定服务。该脚本需要在多台 Linux 机器上运行(有些带有 systemctl,有些没有)。任何帮助将不胜感激

test.sh

#!/bin/bash

serviceName=ngnix.service

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

输出没有 systemctl:

./test.sh: line 5: systemctl: command not found
 [ ? ]  hwclock.sh
 [ - ]  mariadb
 [ - ]  nginx
 [ - ]  nginx-debug
 [ - ]  procps
 [ - ]  redis-server
 [ - ]  rsync

输出与 systemctl:

ngnix.service does NOT exist.

I am trying to get the service whether it is installed or not. The script is working fine with systemctl but fails where systemctl is not installed. As an alternative to that I am using service command to check but by doing so I am unable to run the grep command to grep specific service. This script needs to run on multiple linux machine (some with systemctl and some without it). Any help would be appreciated

test.sh

#!/bin/bash

serviceName=ngnix.service

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

output without systemctl:

./test.sh: line 5: systemctl: command not found
 [ ? ]  hwclock.sh
 [ - ]  mariadb
 [ - ]  nginx
 [ - ]  nginx-debug
 [ - ]  procps
 [ - ]  redis-server
 [ - ]  rsync

output with systemctl:

ngnix.service does NOT exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2025-01-25 17:12:15

将这两个命令放在一个命令组中并将整个命令组重定向到 grep

if { systemctl --all --type service || service --status-all; } 2>/dev/null |
  grep -q "$serviceName"; then
  echo "$serviceName exists."
else
  echo "$serviceName does NOT exist."
fi

Place both commands in a command-group and redirect the whole command-group to grep:

if { systemctl --all --type service || service --status-all; } 2>/dev/null |
  grep -q "$serviceName"; then
  echo "$serviceName exists."
else
  echo "$serviceName does NOT exist."
fi
北笙凉宸 2025-01-25 17:12:15

SystemCTL是用于管理SystemD单元的控制实用程序。您可以尝试在基于Systemd的Init System中检查内部/usr/lib/systemd/usr/lib/systemd/insive nginx的存在。不要忘记理念“一切都是文件”。
请参阅 man systemd.unit.unit 以获取更多解释。
对于使用Init Init System的古代发行版,请参阅Directory /etc/init.d/请参阅男人服务

systemctl is a control utility for managing systemd units. You can try checking nginx presence inside /usr/lib/systemd/ in case of systemd based init system. Don't forget the philosophy "Everything is a file."
See man systemd.unit for more explanation.
For ancient distros with init init system check directory /etc/init.d/ see man service

唠甜嗑 2025-01-25 17:12:15

如果您不想检查系统是否具有SYSV或INTD内部,则可以搜索二进制文件。如果存在二进制,则可以假设安装了软件包和服务。例如:

#!/usr/bin/env bash

is_nginx_exists=$(which nginx)

if [[ $? == 0 ]]; then
        echo "YES"
else
        echo "NO"
fi

说明:

哪个nginx-将尝试从系统宽的系统中找到nginx二进制
二进制清单;

$? - 此命令确定是否执行了以前的命令
成功地。在我们的情况下,0表示二进制;

它是常规VM,Docker Container等的快速,干净和最重要的作品。

If you don't want to check if the system has sysV or initd inside you can search just for the binary. If the binary exists, you can assume that the package and the services are installed. For instance:

#!/usr/bin/env bash

is_nginx_exists=$(which nginx)

if [[ $? == 0 ]]; then
        echo "YES"
else
        echo "NO"
fi

Explanation:

which nginx - will try to find the nginx binary from a system wide
binaries list;

$? - this command determinate if previous command executed
successfully. In our case 0 means that the binary was found;

It's fast, clean and most important works in regular VM, docker container, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文