编写了一个简单的bash脚本,用于检查httpd (apache)或乌德(防病毒)是否运行在我的Centos服务器上,如果没有,它将重新启动它们。
#!/bin/bash
if [[ ! "$(/sbin/service httpd status)" =~ "running" ]]
then
service httpd start
elif [[ ! "$(/sbin/service clamd status)" =~ "running" ]]
then
service clamd start
fi通过命令行对其进行测试,使其工作正常,但是有任何方法可以进一步优化吗?
发布于 2015-08-24 03:11:05
不再关心文本,只需检查返回值即可。
#!/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发布于 2017-03-30 19:13:02
#!/usr/bin/env bash
# First parameter is a comma-delimited string i.e. service1,service2,service3
SERVICES=$1
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 $2}')
if [ "${STATUS}" != "started" ]; then
echo "${service} not started"
#DO STUFF TO SERVICE HERE
fi
donehttps://stackoverflow.com/questions/32173882
复制相似问题