【问题标题】:Why do commands listed in the .sh script are not executed while manual typing works?为什么在手动键入时不执行 .sh 脚本中列出的命令?
【发布时间】:2022-01-25 17:18:45
【问题描述】:

我有使用 Dobby - RDK 实用程序运行的 OCI 运行时捆绑包(在后台使用 crun)。我已经指定我的自定义 entrypoint.sh 脚本在config.json 启动时运行:

"process": {         
        "args": [             
            "sh",               
            "entrypoint.sh"
        ],             
        "cwd": "/",
        "env": [            
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "TERM=xterm"
        ],                  
        "terminal": true,             
        "user": {       
            "uid": 0,    
            "gid": 0     
        }

entrypoint.sh 脚本本身:

#!/bin/sh

while true;
do
sleep 1
lighttpd -v >> lighttpd_version.txt
which lighttpd >> lighttpd_which.txt
lighttpd -f /etc/lighttpd/lighttpd.conf
done

Bundle 结构没问题:

/path_to_bundle
   /config.json
   /rootfs
      ...

我的包基于用于 arm 架构的 Alpine 映像(我添加了一些需要的额外工具)。

让我的容器使用DobbyTool start mycontainer /path_to_bundle 命令运行,我使用nsenter -m -u -i -n -p -t $INIT_CONTAINER_PROCESS_PID 来“进入”容器。

虽然脚本本身正在被执行(我通过 ps 跟踪睡眠进程 - 每次使用不同的 pid,which lighttpd >> lighttpd_which.txt 命令也可以正常工作)但是像 lighttpd -f /etc/lighttpd/lighttpd.conflighttpd -v >> lighttpd_version.txt 这样的命令不会执行。

执行结果:

which lighttpd >> lighttpd_which.txt # shows valid path to command at /usr/sbin/lighttpd

lighttpd -f /etc/lighttpd/lighttpd.conf # neither echoing its result nor getting $! doesn`t work - emptiness

lighttpd -v >> lighttpd_version.txt # doesn`t even create a specified output file

当我在正在运行的容器中手动运行 lighttpd -f /etc/lighttpd/lighttpd.confentrypoint.sh 脚本时,一切正常 - 服务器已启动并正在运行。

编辑:

我也尝试在容器中运行脚本如下:

sh -x entrypoint.sh 2> debug.txt

它根本不会创建那个 debug.txt 文件。手动打字工作正常...

【问题讨论】:

  • 为什么你认为lighttpd -f 没有运行?听起来它正在作为前台进程运行,并阻止脚本的其余部分执行直到它退出/
  • 因为ps看不到。此外,我可以看到睡眠过程的 pid 不断变化。最后它默认在后台运行 - 我没有使用 -D 选项:-D don't go to background (default: go to background)
  • 顺便说一句,我直接使用了 crun 并通过 lighttpd 发现了这个错误:server.c.1391) setgid(): Operation not permitted。似乎这可能是一个问题。然而,尽管没有在脚本中执行,但运行 lighttpd -v 工作正常......

标签: bash


【解决方案1】:

问题是用于运行容器 Dobby 的包装器已将所有命令的输出重定向到某处。另一方面,使用 crun 运行相同的东西(运行时包)可以解决这个问题 - 我收到一条消息,表明该进程的权限不足(特别是没有更改 uid 或 pid 的权限) - lighttpd 实用程序中发生的一些内部事情。因此,在 config.json 的“进程”对象的同名部分下添加一些额外的功能(CAP_SETGIDCAP_SETUID 用于我的具体错误)已经解决了这个问题。

所以我下次在调试类似问题时会做的事情 - 尽可能使用可用的深度工具来检测某些底层级别上可能存在的错误。

【讨论】:

    【解决方案2】:

    尝试用完整路径替换脚本中的lighttpd,即运行which lighttpd 时打印的内容。 例如,如果which lighttpd 打印/usr/sbin/lighttpd,你的脚本应该是这样的:

    #!/bin/sh
    
    while true;
    do
    sleep 1
    /usr/sbin/lighttpd -v >> lighttpd_version.txt
    which lighttpd >> lighttpd_which.txt
    /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
    done
    

    【讨论】:

    • 感谢您的回复。不幸的是,我一直在尝试这样做 - 没有结果。这也不应该成为问题,因为所需的路径列在 config.jsonenv object 中。顺便说一句,有时我可以启动并运行该 lighttpd 命令(但这就像在蓝月亮中发生的那样),但我无法重现它。基本上我什么都没改变 - 这就是为什么......
    猜你喜欢
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2013-10-31
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    相关资源
    最近更新 更多