1
0

fix: 优化omcd脚本,增加服务存在性检查

This commit is contained in:
TsMask
2025-04-21 16:27:49 +08:00
parent 38f6652301
commit 9ee596f01d

View File

@@ -1,28 +1,53 @@
#!/bin/bash #!/bin/bash
ServiceList="omc sshsvc" ServiceList="omc sshsvc"
BinDir="/usr/local/bin"
# Check if the service exists
check_service() {
systemctl list-unit-files | grep -q "$1.service"
return $?
}
case "$1" in case "$1" in
start) start)
for v in $ServiceList;do for v in $ServiceList; do
if ! check_service "$v"; then
echo "Service $v does not exist"
continue
fi
systemctl start $v systemctl start $v
if [ $? = 0 ]; then if [ $? = 0 ]; then
echo "starting $v process done" echo "Starting $v process done"
fi else
echo "Failed to start $v"
fi
sleep 1 sleep 1
done done
;; ;;
status) status)
for v in $ServiceList;do for v in $ServiceList; do
systemctl status $v if ! check_service "$v"; then
echo "Service $v does not exist"
continue
fi
echo "=== Status of $v ==="
systemctl status --no-pager -n 20 $v
echo ""
done done
;; ;;
stop) stop)
for v in $ServiceList;do for v in $ServiceList; do
systemctl stop $procName if ! check_service "$v"; then
echo "Service $v does not exist"
continue
fi
systemctl stop $v
if [ $? = 0 ]; then if [ $? = 0 ]; then
echo "Stopping $v process done" echo "Stopping $v process done"
fi else
echo "Failed to stop $v"
fi
done done
;; ;;
restart) restart)
@@ -31,9 +56,12 @@ case "$1" in
$0 start $0 start
;; ;;
version) version)
local BinDir=/usr/local/bin for v in $ServiceList; do
for v in $ServiceList;do if [ -x "$BinDir/$v" ]; then
$BinDir/$v --version $BinDir/$v --version
else
echo "$v binary not found in $BinDir"
fi
done done
;; ;;
*) *)