for xxx in xxxs
do
## 执行语句
## 一般执行语句是要写封装好的启停脚本或者是一些自定义的输出日志
done
xxxs
一般是主机IP
例子:
这个脚本是分别在(hadoop102 hadoop103 hadoop104)的/opt/module/zookeeper/bin/目录下启动zkServer.sh start命令,然后输出日志("$host zookeeper Server 正在启动......")#!/bin/bash
for host in hadoop102 hadoop103 hadoop104
do
ssh $host "source /etc/profile;/opt/module/zookeeper/bin/zkServer.sh start"
echo "$host zookeeper Server 正在启动......"
done
函数名 () {
## 方法体
## 一般是启动和关闭组件的一些命令
}
if else
if 逻辑判断; then
## 执行语句
else
## 执行语句
fi
case
case $1 in
传递给脚本的参数 )
# 执行语句(一般是一个方法)
;;
传递给脚本的参数 )
# 执行语句(一般是一个方法)
;;
传递给脚本的参数 )
# 执行语句(一般是一个方法)
;;
esac
$1
语句传递给脚本的第一个参数传递给脚本的参数
一般是start,stop,restart 之类的这个脚本是一个用于管理Maxwell服务的简单Shell脚本,包含启动、停止和重启功能
#!/bin/bash
MAXWELL_HOME=/opt/module/maxwell
status_maxwell(){
result=`ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | wc -l`
return $result
}
start_maxwell(){
status_maxwell
if [[ $? -lt 1 ]]; then
echo "启动Maxwell"
$MAXWELL_HOME/bin/maxwell --config $MAXWELL_HOME/config.properties --daemon
else
echo "Maxwell正在运行"
fi
}
stop_maxwell(){
status_maxwell
if [[ $? -gt 0 ]]; then
echo "停止Maxwell"
ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | awk '{print $2}' | xargs kill -9
else
echo "Maxwell未在运行"
fi
}
case $1 in
start )
start_maxwell
;;
stop )
stop_maxwell
;;
restart )
stop_maxwell
start_maxwell
;;
esac
MAXWELL_HOME=/opt/module/maxwell
MAXWELL_HOME
,指定了Maxwell安装的目录。status_maxwell
status_maxwell(){
result=`ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | wc -l`
return $result
}
ps -ef
列出所有进程。grep com.zendesk.maxwell.Maxwell
查找与Maxwell相关的进程。grep -v grep
确保不包括自己。wc -l
计算返回的行数,即运行Maxwell的进程数。return $result
将行数作为返回值(返回0表示没有启动,返回1表示启动了)。start_maxwell
start_maxwell(){
status_maxwell
if [[ $? -lt 1 ]]; then
echo "启动Maxwell"
$MAXWELL_HOME/bin/maxwell --config $MAXWELL_HOME/config.properties --daemon
else
echo "Maxwell正在运行"
fi
}
status_maxwell
函数中检查Maxwell状态。stop_maxwell
stop_maxwell(){
status_maxwell
if [[ $? -gt 0 ]]; then
echo "停止Maxwell"
ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | awk '{print $2}' | xargs kill -9
else
echo "Maxwell未在运行"
fi
}
kill -9
强制终止它们。awk '{print $2}'
awk 是一个强大的文本处理工具,这里用来提取进程信息。{print $2} 指的是输出每行的第二列,通常这是进程ID。xargs
将标准输入的数据转换为命令行参数的工具。在这里,它将提取到的PID传递给 kill -9。kill
用于终止进程-9
强制杀死进程的信号case $1 in
start )
start_maxwell
;;
stop )
stop_maxwell
;;
restart )
stop_maxwell
start_maxwell
;;
esac
case
语句处理传递给脚本的第一个参数(如 start
, stop
, restart
)。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。