检查服务是否已安装在Linux中
我正在尝试获取该服务,无论它是否安装。该脚本在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将这两个命令放在一个命令组中并将整个命令组重定向到
grep
:Place both commands in a command-group and redirect the whole command-group to
grep
: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如果您不想检查系统是否具有SYSV或INTD内部,则可以搜索二进制文件。如果存在二进制,则可以假设安装了软件包和服务。例如:
说明:
它是常规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:
Explanation:
It's fast, clean and most important works in regular VM, docker container, etc.