可以看到pid在第二列,这里需要注意。
#!/bin/bash #mysql的所有进程的PID pid=$(ps aux | grep "mysql" | awk ‘{print $2}‘) echo "mysql所有相关PID:"$pid
分析: ps是查看指令, grep是条件 ,awk是打印我们需要的字段PID在第二个字段 print $2 ,print $1就会是root。
我们加入限制条件查找local下的mysq
#!/bin/bash pid=$(ps aux | grep "mysql" | awk ‘{print $2}‘) echo "mysql的所有进程的PID:" $pid #第二组 pid2=$(ps aux | grep ‘mysql‘ | grep ‘local‘ | awk ‘{print $2}‘) echo "local下的mysql进程PID:"$pid2 #第三组 pid2=$(ps aux | grep ‘mysql‘ | grep ‘local‘ | awk ‘{print $1}‘) echo "打印第一个列内柔:"$pid2
#!/bin/bash master_id=$(docker ps -a|grep "master" |awk ‘{print $1}‘) echo "=====The master containerId is :"$master_id========= slave01_id=$(docker ps -a |grep "slave01"|awk ‘{print $1}‘) echo "=====The slave01 containerId is :"$slave01_id======= slave02_id=$(docker ps -a |grep "slave02"|awk ‘{print $1}‘) echo "=====The slave02 containerId is :"$slave02_id======= docker restart $master_id docker restart $slave01_id docker restart $slave02_id echo "---------------successful to start master,slave01 and slave02-----------------------"
这里利用start和restart的效果应该是一致的,我后来也写了一个关闭所有相关容器的脚本,与这个大同小异。
#!/bin/bash #运行mysql docker run -d -p 4406:3306 registry.cn-hangzhou.aliyuncs.com/zhufc/mysql:v2 docker ps #获取到ID id=$(docker ps |grep "mysql" | awk ‘{print $1}‘) echo "容器id:"$id #进入容器 docker exec -it $id bash #打印当前的路径 pwd
原文:https://www.cnblogs.com/codehero/p/13204989.html