shell的exec命令

概述

  • 在docker的启动脚本中,我们经常可以看到有exec来启动执行命令,那这个到底有什么作用呢?我们来简单验证一下。
#!/bin/bash

set -euo pipefail

# Check if the the user has invoked the image with flags.
# eg. "apm-server -c apm-server.yml"
if [[ -z $1 ]] || [[ ${1:0:1} == '-' ]] ; then
  exec apm-server "$@"
else
  # They may be looking for a Beat subcommand, like "apm-server setup".
  subcommands=$(apm-server help \
                  | awk 'BEGIN {RS=""; FS="\n"} /Available Commands:/' \
                  | awk '/^\s+/ {print $1}')

  # If we _did_ get a subcommand, pass it to apm-server.
  for subcommand in $subcommands; do
      if [[ $1 == $subcommand ]]; then
        exec apm-server "$@"
      fi
  done
fi

# If neither of those worked, then they have specified the binary they want, so
# just do exactly as they say.
exec "$@"
exec作用
在一个shell里面,里面的所有的命令都是这个shell的子进程。
  • 如下测试,在demo.sh中输入以下内容
  #!/bin/sh
  sleep 10 
  • 执行demo.sh,使用pstree观察进程关系,如下
# pstree  52362
sh───sleep
如果执行到了exec的子命令,那么当前的shell将被替换掉,当前的shell将会被退出,后续的脚本无法继续执行
  • 如下测试,在demo.sh中输入以下内容
  #!/bin/sh
  exec sleep 10 
  • 执行demo.sh,使用pstree观察进程关系,如下
# pstree 52692
sleep
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值