本文介绍了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)
发布评论