Error while running parallel make

2019-05-05 04:03发布

问题:

Consider following make:

all: a b

a:
        echo a
        exit 1


b:
        echo b start
        sleep 1
        echo b end

While running it as make -j2 I receive the following output:

echo a
echo b start
a
exit 1
b start
sleep 1
make: *** [a] Error 1
make: *** Waiting for unfinished jobs....
echo b end
b end

We have quite a big make file and it's easy to miss error, since there's no error message at the end of execution.

Is there a way for error message to appear also just in the end of the make execution?

UPDATE:

See my possible solution how to check make exit status from within make.

回答1:

If any program called by make returns with an error, the return code of make will not be zero. So after calling make you could just check the return code to see if an error occurred. On bash (and many other shells like zsh) you could do the following:

# make
....
# echo $?

The echo will print 0 if anything was ok and will print something else if it wasn't.

You could also run the check from inside a shell script:

make
if [ $? -eq 0 ]; then
    echo "Everything OK"
else
    echo "Something went wrong!"
fi

If you need the exact error message the easiest way is to redirect the output from make to some file and grep for the error if execution failed.

But the way I usually do this is to run make in parallel and re-execute it with -j1 if something went wrong so I will get a clean error message. I guess this could be put into a shell script using the technique above.



回答2:

What you're seeing is that the output of both these tasks are independent. So, if you're running 5 tasks in parallel, and 3 of them gave errors, they may very well be interspersed by output of the other tasks.

The best thing to do is run a set of serial tasks together, send their error output into a file... and print it to screen when exiting.

P.S. - Also, never use exit 1 in a task unless there absolutely is an error!