Bash-启动运行CentOS 6.8的多个服务(如果有)
我想检查是否有一个(或所有)服务正在运行,如果是,请停止它Bash-启动运行CentOS 6.8的多个服务(如果有),bash,shell,unix,Bash,Shell,Unix,我想检查是否有一个(或所有)服务正在运行,如果是,请停止它 #!/bin/bash # Define an array of processes to be checked. # If properly quoted, these may contain spaces check_process=( "nagios" "httpd" ) for p in "${check_process[@]}"; do if pgrep "$p" > /dev/null; then
#!/bin/bash
# Define an array of processes to be checked.
# If properly quoted, these may contain spaces
check_process=( "nagios" "httpd" )
for p in "${check_process[@]}"; do
if pgrep "$p" > /dev/null; then
echo "Process \`$p' is running, stopping it"
service $p stop
else
echo "Process \`$p' is not running"
fi
done
对于httpd服务,脚本可以正确检测httpd
服务状态。
我在检测nagios服务状态时遇到问题。
但是,尽管nagios
服务没有运行,但脚本显示它正在运行
Process `nagios' is running, stopping it
Stopping nagios:No lock file found in /usr/local/nagios/var/nagios.lock
Process `httpd' is not running
有没有更优雅的方法来检测nagios服务是否正在运行,而不检查nagios.lock
文件是否存在
pgrep nagios
在服务未启动时不显示输出。我放弃了,这对我来说很好:
虽然ps-ef | grep-v grep | grep$service | wc-l
显示nagios的0,但脚本报告nagios服务正在运行
#!/bin/bash
logfile=/tmp/stop_nagios.txt
exec >> $logfile
exec 2>&1
service=httpd
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running, stopping it"
date
sudo service $service stop
else
echo "$service is not running"
fi
# check nagios service
FILE=/usr/local/nagios/var/nagios.lock
if test -f "$FILE"; then
echo "nagios service is running, stopping it"
date
sudo service nagios stop
else
echo "nagios is not running..."
fi
使用状态:。或者直接调用
stop
,如果它已经停止了,它将什么也不做。正如Nic3500所说,现在只有大多数系统使用systemd
,看看命令systemctl
。我使用的是CentOS 6,没有systemctl,为什么不直接运行service“$p”无条件停止
?如果没有其他帮助,我会这样做