Jus*_*tin 17 linux bash ubuntu service centos
bash检查服务是否安装的最佳方法是什么?它应该适用于Red Hat(CentOS)和Ubuntu?
思维:
service="mysqld"
if [ -f "/etc/init.d/$service" ]; then
# mysqld service exists
fi
Run Code Online (Sandbox Code Playgroud)
也可以使用该service命令并检查返回码.
service mysqld status
if [ $? = 0 ]; then
# mysqld service exists
fi
Run Code Online (Sandbox Code Playgroud)
什么是最好的解决方案?
Joe*_*l B 13
Rustam Mamat因此得到了赞誉:
如果列出所有服务,您可以查看结果以查看其中的内容.例如:
# Restart apache2 service, if it exists.
if service --status-all | grep -Fq 'apache2'; then
sudo service apache2 restart
fi
Run Code Online (Sandbox Code Playgroud)
要获得一项服务的状态而不“ ping”所有其他服务,可以使用以下命令:
systemctl list-units --full -all | grep -Fq "$SERVICENAME.service"
Run Code Online (Sandbox Code Playgroud)
顺便说一下,这就是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 }' ) )
Run Code Online (Sandbox Code Playgroud)
或更详细的解决方案:
service_exists() {
local n=$1
if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | cut -f1 -d' ') == $n.service ]]; then
return 0
else
return 1
fi
}
if service_exists systemd-networkd; then
...
fi
Run Code Online (Sandbox Code Playgroud)
希望对您有所帮助。
在 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
Run Code Online (Sandbox Code Playgroud)
在新贵系统上:
serviceName="Name of your service"
if initctl list | grep -q "$serviceName";then
echo "$serviceName exists."
else
echo "$serviceName does NOT exist."
fi
Run Code Online (Sandbox Code Playgroud)
在 SysV(System V)系统上:
serviceName="Name of your service"
if service --status-all | grep -q "$serviceName";then
echo "$serviceName exists."
else
echo "$serviceName does NOT exist."
fi
Run Code Online (Sandbox Code Playgroud)
在 systemd 中(尤其是在 Debian 中),使用此处的各种答案似乎无法正常工作。对于某些服务,例如pure-ftpd处于禁用模式的服务,当您触发此命令时,它不会显示在服务列表中:
systemctl --all --type service
当您再次开始时,pure-ftpd列表systemctl start pure-ftpd将再次出现。因此列出使用的服务systemctl --all --type service并不适用于所有服务。查看此内容以获取更多信息。
因此,这是迄今为止最好的代码(对 @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
Run Code Online (Sandbox Code Playgroud)
解释:
如果systemctl status找到一个服务,它必须有一个文本“Active:”,我们使用 grep 进行过滤,它将返回 0。如果没有“Active:”文本,它将返回 1。
如果systemctl status没有找到“Active:”文本,它将打印出一个标准错误。因此,我使用重定向2> /dev/null来重定向标准错误。例如,如果您正在寻找不存在的服务,如果您不设置该错误重定向,您将收到此错误消息:
Unit pure-ftpdd.service could not be found.
Run Code Online (Sandbox Code Playgroud)
如果您正在编写脚本,我们不希望出现上述标准错误消息
编辑:
另一种方法是列出能够检测 Debian 系统的@Anthony Rutledge 指出的禁用服务的单元文件:
systemctl list-unit-files --type service | grep -F "pure-ftpd"
Run Code Online (Sandbox Code Playgroud)
但使用此方法并不总是有效,特别是对于较旧的系统,因为使用此命令可能无法检测到某些单元文件,如此处所述。此外,如果您有需要过滤的大型单元文件,则使用此方法会更慢(正如 @ygoe 关于小型计算机上的重负载所评论的那样)。
Rus*_*tam -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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27891 次 |
| 最近记录: |