Published on

Forcing a Linux Command to Always Return a Success Code

Authors

In a makefile I was updating today I had a sequence of targets that are pulled together in one target and I wanted all to run even if some of them failed:

clean-image-1:
    docker rm -f image1
clean-image-2:
    docker rm -f image2
clean-all-images: clean-image-1 clean-image-2

The problem is in the above if image1 does not exist the clean-image-1 target will fail when calling clean-all-images and clean-image-2 will not run. As this type of error does not indicate a fatal error I want to be able to force this to return a success error code even if the command failed. After a bit of searching I came across this answer. The solution is simple append true to any command that may fail which you want to return a success error code always: commandThatMayFail; true. Or with the docker example it would be: docker rm -f someImageThatMayOrNotExist; true.

Using this approach I updated my makefile as follows:

clean-image-1:
    docker rm -f image1; true
clean-image-2:
    docker rm -f image2; true
clean-all-images: clean-image-1 clean-image-2