Bash:运行 服务,如果尚未 运行ning(Centos、Apache、Clam)
Bash: run service if not already running (Centos, Apache, Clam)
写了一个简单的 bash 脚本 运行 以检查 httpd (apache) 或 clamd (antivirus) 是否在我的 Centos 服务器上 运行ning,并且如果不是,它将重新启动它们。
#!/bin/bash
if [[ ! "$(/sbin/service httpd status)" =~ "running" ]]
then
service httpd start
elif [[ ! "$(/sbin/service clamd status)" =~ "running" ]]
then
service clamd start
fi
通过命令行对其进行了测试,因此它可以工作,但是有什么方法可以进一步优化它吗?
不再关心文本,只需检查 return 值。
#!/bin/sh
service httpd status &> /dev/null || service httpd start
service clamd status &> /dev/null || service clamd start
或者只是不关心它们已经 运行 让系统处理它。
#!/bin/sh
service httpd start
service clamd start
#!/usr/bin/env bash
# First parameter is a comma-delimited string i.e. service1,service2,service3
SERVICES=
if [ $EUID -ne 0 ]; then
if [ "$(id -u)" != "0" ]; then
echo "root privileges are required" 1>&2
exit 1
fi
exit 1
fi
for service in ${SERVICES//,/ }
do
STATUS=$(service ${service} status | awk '{print }')
if [ "${STATUS}" != "started" ]; then
echo "${service} not started"
#DO STUFF TO SERVICE HERE
fi
done
写了一个简单的 bash 脚本 运行 以检查 httpd (apache) 或 clamd (antivirus) 是否在我的 Centos 服务器上 运行ning,并且如果不是,它将重新启动它们。
#!/bin/bash
if [[ ! "$(/sbin/service httpd status)" =~ "running" ]]
then
service httpd start
elif [[ ! "$(/sbin/service clamd status)" =~ "running" ]]
then
service clamd start
fi
通过命令行对其进行了测试,因此它可以工作,但是有什么方法可以进一步优化它吗?
不再关心文本,只需检查 return 值。
#!/bin/sh
service httpd status &> /dev/null || service httpd start
service clamd status &> /dev/null || service clamd start
或者只是不关心它们已经 运行 让系统处理它。
#!/bin/sh
service httpd start
service clamd start
#!/usr/bin/env bash
# First parameter is a comma-delimited string i.e. service1,service2,service3
SERVICES=
if [ $EUID -ne 0 ]; then
if [ "$(id -u)" != "0" ]; then
echo "root privileges are required" 1>&2
exit 1
fi
exit 1
fi
for service in ${SERVICES//,/ }
do
STATUS=$(service ${service} status | awk '{print }')
if [ "${STATUS}" != "started" ]; then
echo "${service} not started"
#DO STUFF TO SERVICE HERE
fi
done