Bash:运行服务(如果尚未运行)(Centos,Apache,Clam) [英] Bash: run service if not already running (Centos, Apache, Clam)

查看:80
本文介绍了Bash:运行服务(如果尚未运行)(Centos,Apache,Clam)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个将要运行的简单bash脚本,以检查httpd(apache)或clamd(防病毒)是否在我的Centos服务器上运行,如果没有运行,它将重新启动它们.

Wrote a simple bash script which is to be run in order to check if httpd (apache) or clamd (antivirus) is running on my Centos server, and if not it will restart them.

#!/bin/bash
if [[ ! "$(/sbin/service httpd status)" =~ "running" ]]
then
    service httpd start
elif [[ ! "$(/sbin/service clamd status)" =~ "running" ]]
then 
    service clamd start
fi

通过命令行对其进行了测试,因此可以正常工作,但是有没有办法进一步优化呢?

tested it via the command line so it works, but is there any way to optimize this further?

推荐答案

不要关心文本,只需检查返回值即可.

Stop caring about the text and just check the return values.

#!/bin/sh
service httpd status &> /dev/null || service httpd start
service clamd status &> /dev/null || service clamd start

或者只是不在乎他们已经在运行,让系统来处理它.

Or just don't care that they're already running and let the system handle it.

#!/bin/sh
service httpd start
service clamd start

这篇关于Bash:运行服务(如果尚未运行)(Centos,Apache,Clam)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆