[英]Check if service exists in bash (CentOS and Ubuntu)
在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
最好的解決方案是什么?
要在不“ping”所有其他服務的情況下獲取一項服務的狀態,您可以使用以下命令:
systemctl list-units --full -all | grep -Fq "$SERVICENAME.service"
順便說一下,這是 bash (auto-)completion 中使用的內容(請參閱文件 /usr/share/bash-completion/bash_completion,查找 _services):
COMPREPLY+=( $( systemctl list-units --full --all 2>/dev/null | \
awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' ) )
或者更詳細的解決方案:
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
希望有所幫助。
Rustam Mamat 對此功不可沒:
如果您列出所有服務,您可以 grep 結果以查看其中的內容。 例如:
# Restart apache2 service, if it exists.
if service --status-all | grep -Fq 'apache2'; then
sudo service apache2 restart
fi
在 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(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
在 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
解釋:
如果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"
但是使用此方法並不總是適用於較舊的系統,因為使用此命令可能無法檢測到某些單元文件,如此處所述。 此外,如果您有需要過濾的大型單元文件(如@ygoe 所評論的關於小型計算機上的重負載),則使用此方法會較慢。
為了建立喬爾 B 的答案,這里是一個函數(增加了一點靈活性。注意完全沒有參數檢查;如果你不傳入 2 個參數,這會中斷):
#!/bin/sh
serviceCommand() {
if sudo service --status-all | grep -Fq ${1}; then
sudo service ${1} ${2}
fi
}
serviceCommand apache2 status
#!/bin/sh
service=mysql
status=$(/etc/init.d/mysql status)
print "$status"
#echo $status > /var/log/mysql_status_log
在閱讀了一些 systemd 手冊頁后...
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
組合來確定系統服務的狀態。
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
如果服務確實存在
試試這個,因為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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.