consul lock option to return child process exit code

Created on 16 May 2015  路  10Comments  路  Source: hashicorp/consul

I've got a script, call it foo.sh, that I want to wrap in a lock and then check the exit code for errors:

consul lock foo ./foo.sh
if [[ $? != 0 ]]; then ./bar.sh; fi

Currently this will check the exit code of consul, not foo.sh. It would be useful to have an option to make consul set its exit code equal to the exit code of the child process.

The workaround for now is to have the child process write its exit code to a a file:

consul lock './foo.sh; echo $?>foo_sh_exit_code'
if [[ `cat foo_sh_exit_code` != 0 ]]; then ./bar.sh; fi

Which seems a bit messy to me, but perhaps I just need more bash zen.

themcli typenhancement

Most helpful comment

I think a -child-exitcode might make sense here, marking as an enhancement!

All 10 comments

I think a -child-exitcode might make sense here, marking as an enhancement!

+1

This would be very useful to serialize some checks we run, but I need the return code to confirm the check result.

Any chance this will be merged in?

@dpkirchner there's one outstanding thing with the PR and it needs a rebase. I'll try to get this in before the next release - it's super close.

Looking forward to not having to use this workaround :)

+1 would love to have this in :)

@slackpad _bump_ :)

Would be very useful have this feature in the next release :)

I found a slightly nicer (yet still ugly) workaround by the way, but I'd also prefer not having to use it :)

#!/bin/bash

set -e
set -o pipefail

consul lock $@ | awk '{ print $0; while ( getline == 1 ) { print $0; } if ( $0 ~/exit status/ ) { exit 2; } }'
~$ ./consul_lock lock_key false
Error running handler: exit status 1
exit status 1
~$ echo $?
2

Using this one it's also possible to differentiate between failure of lock acquisition (exit code 1) and failure of the child process (exit code 2).

I just wrote a very wrong script because I assumed that consul lock would automatically propagate the child exit code.

yup, made that mistake as well. by the way my hack does not work anymore with >0.8

Was this page helpful?
0 / 5 - 0 ratings