简体   繁体   English

如何在 bitbake 配方上捕获命令的退出代码?

[英]How can I catch exit codes of a command on a bitbake recipe?

I have a bitbake recipe in which I need to check for the availability of a remote server before downloading some packages from it.我有一个 bitbake 配方,在从中下载一些包之前,我需要检查远程服务器的可用性。 For that, I use ping as below:为此,我使用 ping 如下:

ping ${HOST} -c1 -w4 1>/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
    echo "ERROR: Unable to reach ${HOST}. Exiting now with code $?..."
    exit $? 
fi

The code above works just fine in a terminal, and I get the corresponding exit codes: 0 for OK and nonzero for NOK.上面的代码在终端中工作得很好,我得到了相应的退出代码:0 表示 OK,非零表示 NOK。

However, the exact same code on a bitbake recipe, the exit code $?但是,bitbake 配方上的代码完全相同,退出代码$? is always empty.总是空的。 Instead, bitbake itself will catch the error code, and the execution will continue.相反,bitbake 本身会捕获错误代码,然后继续执行。 It will fail much later, when trying to unpack the not-downloaded files.稍后,当尝试解压缩未下载的文件时,它会失败。 At that point, I get a warning about the nonzero exit code thrown by ping much earlier.那时,我收到了一条关于ping更早抛出的非零退出代码的警告。 Currently that is how it looks like:目前这就是它的样子:

if [ "$(ping ${HOST} -c1 -w4 1>/dev/null 2>/dev/null)" = 0 ]; then
    echo "ERROR: Unable to reach ${HOST}. Exiting now..."
    exit 1 
fi

# Some other stuff here...

ar -x ${BUILDDIR}/tmp/deploy/ipk/all/rheas_*.ipk

And I get:我得到:

ERROR: rheas-0.0-r0 do_compile: Function failed: do_compile (log file is located at /data/oe-core/build/tmp/work/armv5te-poky-linux-gnueabi/rheas/0.0-r0/temp/log.do_compile.2239)
ERROR: Logfile of failure stored in: /data/oe-core/build/tmp/work/armv5te-poky-linux-gnueabi/rheas/0.0-r0/temp/log.do_compile.2239
Log data follows:
| DEBUG: Executing shell function do_compile
| ar: /data/oe-core/build/tmp/deploy/ipk/all/rheas_*.ipk: No such file or directory
| WARNING: exit code 9 from a shell command.
| ERROR: Function failed: do_compile (log file is located at /data/retail-renos-oe-core/build/tmp/work/armv5te-poky-linux-gnueabi/rheas/0.0-r0/temp/log.do_compile.2239)
ERROR: Task (/data/oe-core/meta-renos/recipes-core/rheas/rheas_0.0.bb:do_compile) failed with exit code '1'

In summary, I can not use the exit codes myself, because it seems that bitbake is hijacking it somehow.总之,我自己不能使用退出代码,因为 bitbake 似乎以某种方式劫持了它。

The issue with that is that I can't throw a user friendly error, and others never knows where the problem comes from.问题是我不能抛出一个用户友好的错误,而其他人永远不知道问题出在哪里。

So my question is: how can I use exit codes inside a bitbake recipe?所以我的问题是:如何在 bitbake 配方中使用退出代码?

In this project specifically I am using bitbake version 1.32.0.在这个项目中,我特别使用了 bitbake 版本 1.32.0。

This answer does not seem to be in the manual.这个答案似乎不在手册中。

bitbake uses the safer set -e by default: the script execution stops on first error. bitbake 默认使用更安全的set -e :脚本执行在第一个错误时停止。

You could disable this (with set +e ) but I suggest handling the single known-to-fail command specifically instead.您可以禁用此功能(使用set +e ),但我建议改为专门处理单个已知失败的命令。 There's a few ways you can do it, here's an example (this also fixes a bug in your code where you used the exit value of echo as your exit value):有几种方法可以做到这一点,这是一个示例(这也修复了代码中的一个错误,您使用 echo 的退出值作为退出值):

err=0
ping ${HOST} -c1 -w4 1>/dev/null 2>/dev/null || err=$?
if [ $err -ne 0 ]; then
    echo "ERROR: Unable to reach ${HOST}. Exiting now with code $err..."
    exit $err 
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在bitbake的配方中将文件夹或文件添加到根目录? - How can I add a folder or file to the root in a recipe with bitbake? 如何在Yocto / Bitbake的另一个食谱中引用/找到一个食谱的$ {PV}? - How can I reference/find the ${PV} of one recipe in another recipe in Yocto/Bitbake? 如何更改基于autotools的Bitbake配方的安装路径? - How can I change the installation path of an autotools-based Bitbake recipe? 在bitbake配方中运行命令,就像在实时系统上一样 - Run a command in bitbake recipe as if on live system 如何从 bitbake 配方中提取许可信息 - How to extract licensing information from a bitbake recipe 如何在 Bitbake 函数中为每个配方执行代码? - How to execute code for each recipe in a Bitbake function? 如何在 Yocto bitbake 配方中调用 python 模块? - How to invoke a python module in Yocto bitbake recipe? bitbake 如何在构建过程中搜索配方? - How bitbake searches for recipe in build process? 如何使用Bitake配方中的$ {BASEWORK}目录传递给CMAKE,以使用CMAKE的查找库功能? - How can we pass a ${BASEWORK} directory from Bitbake recipe to CMAKE for using find library function from CMAKE? 如何编写 yocto/bitbake 配方以将目录复制到目标根文件系统 - How do I write a yocto/bitbake recipe to copy a directory to the target root file system
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM