Docker非交互式运行命令

发布时间 2023-08-28 18:06:57作者: 辰令

Docker

交互式 shell - interactive shell
  docker exec 最常见的用法。你可以使用 -it 参数启动一个交互式 shell,如 /bin/bash 或 /bin/sh,然后在容器内部执行命令
非交互式执行--用来执行预先设定的命令
   单个命令   
脚本和单个命令
    执行脚本的shell都是“非交互式”的,但我们也有办法把它启动为“交互式”shell,方法就是在执行bash命令时,添加-i选项:
 login logout exit		
   logout退出“登陆shell”,使用exit退出“非登录shell”
   登录shell”执行的startup文件为~/.bash_profile,而“非登陆shell”执行的startup文件为~/.bashrc。
   通过--noprofile选项可以阻止系统加载“profile”系列的startup文

在Docker容器中运行一个非交互式命令

 如果你需要在一个正在运行的Docker容器内运行一个命令,但不需要任何交互性,可以使用不带任何标志的docker exec 命令     
 docker   info 检查docker引擎
 rostopic list 检查ros引擎  
 docker inspect contain_nm | grep "Running"
 docker ps --filter name=nostalgic_stallman
 docker ps --filter status=running  --filter name=mycontain_nm 
 docker ps --filter status=running  --filter name=^/mycontain_nm$
 
 docker ps --filter name=^/mycontain_nm$
 
 $(docker ps -a --filter name=^/mycontain_nm$|wc -l)==2
  精确匹配 name 为 bingohuang 的容器。注意,容器实际名称,开头是有一个正斜线 / 

非交互式执行docker命令

  ## 要在容器的某个目录下运行命令,可以使用--workdir 标志来指定目录
  ## 把环境变量和要运行的命令一起传入容器。-e 标志可以让你指定一个环境变量 个充满环境变量的文件,你可以用--env-file 标志来做
  ## 在后台运行命令:如果你不想在终端中看到命令的输出,你可以使用 -d 参数在后台运行命令
  ## 以特定用户身份运行命令:如果你的 Docker 容器有多个用户,你可以使用 -u 参数以特定用户身份运行命令。
  docker exec contain_nm sed -n '6p' /usr/local/scene_nm/share/test/conf/fig.yaml
  docker exec contain_nm sed -n '2p' /usr/local/scene_nm/share/test/conf/fig.yaml

检查topic有没有

###检查topic有没有消息
检查topic 的消息频次
 rosmsg info 
 rostopic list 
 rosbag info 

步骤

是否存在-存在是否启动
检查容器的状态-确保容器都启动了
修改容器的配置
检查topic的message-确保topic都有消息

脚本

##查看容器是否存在
if [ $(docker ps -a --filter name=^/mycontain_nm.0.2$ |wc -l) == 2 ]; then
    echo "Have mycontain_nm.0.2 "
else
    echo "No mycontain_nm container "
fi

##查看容器是否启动
if [ $(docker ps --filter name=^/mycontain_nm.0.2$ --filter status= running|wc -l) == 2 ]; then
    echo "mycontain_nm.0.2 running "
else
    echo "mycontain_nm container Not running "
fi

For 循环查看多个容器是否启动running

#!/bin/bash
i=0
for file in mycontain_nm your_model.1 other_cfg_1 
do
    let i++
	if [ $(docker ps -a --filter status=running --filter name=^$file$ |wc -l) == 2 ];then
	    echo "$i : $file container Running "
	else
	   echo  "$i : $file container Not Running !!"
    fi
done

检查Ros 的topic 是否存在,是否有消息

#!/bin/bash
i=0
for file_nm in /mytopic_1 /mytopic_10
do 
    let i++
    result=$(timeout 2s rostopic echo -n 2 $file_nm); 
    echo  "$i "	
    if [[ $result != "" ]] ; then
	   echo "$i:$file_nm topic have msg" ;
	else
	   echo "$i:$file_nm no msg  !!"
	fi;
done;

查看状态

################ get.sh ##############################
#!/bin/bash
test_number=$(echo $DEV_ID | cut -d '_' -f2)
echo $test_number
docker exec mycontain_nm sed -n '6p' /usr/local/fig.yaml


###############改变 #################
#!/bin/bash
test_number=$(echo $DEV_ID | cut -d '_' -f2)
echo $test_number
docker exec -e test_number=$(echo $DEV_ID | cut -d '_' -f2) mycontain_nm sed -i '3s/topic: \/info\/map_at/topic: \/info\/map_at_'${test_number}'/' /usr/local/fig.yaml
docker exec -e test_number=$(echo $DEV_ID | cut -d '_' -f2) mycontain_nm sed -i '6s/mymap: false/mymap: true/' /usr/local/fig.yaml
docker restart mycontain_nm
docker exec mycontain_nm sed -n '6p' /usr/local/fig.yaml
###说明:
    Shell 中, 可以用等号 = 来定义变量。注意,等号两边不能有空格

Ros采集数据

timeout 30s rosbag record \
 /CAM01/camera/image_raw \
 /CAM02/camera/image_raw \ 
 --duration=3s  --split  -o  /data/test.bag

运行结束后查看 Ros采集的bag包

find ./ -maxdepth 1 -type f -iname "*.bag" | while read dir; do count=$(rosbag info -y -k topics $dir |wc -l); echo "$dir : $count "; done 	
find ./ -maxdepth 1 -type f -iname "*.bag" | while read file_nm;do cnt=$(rosbag info -y -k topics  $file_nm|grep "\-\ topic" |wc -l);echo "$file_nm: $cnt";done	

Linux命令说明

 linux系统中let命令在bash中用于计算,变量名前不用加$,可以实现自加和自减操作
   let 命令用于计算赋值表达式。它允许用户在脚本中定义变量并对其进行操作,同时将结果赋值给另一个变量
   arg中的运算符和操作数之间有空格,否则会产生错误
 echo cut  sed  timeout
 for in do if then else fi done
 docker ros 

可视化选项

 gnome-terminal:启动终端