I have a bitbake recipe in which I need to check for the availability of a remote server before downloading some packages from it. For that, I use ping as below:
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.
However, the exact same code on a bitbake recipe, the exit code $?
is always empty. Instead, bitbake itself will catch the error code, and the execution will continue. 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. 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.
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?
In this project specifically I am using bitbake version 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.
You could disable this (with set +e
) but I suggest handling the single known-to-fail command specifically instead. 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):
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