Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consul lock option to return child process exit code #947

Closed
dpkp opened this issue May 16, 2015 · 10 comments
Closed

consul lock option to return child process exit code #947

dpkp opened this issue May 16, 2015 · 10 comments
Assignees
Labels
theme/cli Flags and documentation for the CLI interface type/enhancement Proposed improvement or new feature

Comments

@dpkp
Copy link

dpkp commented May 16, 2015

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.

@ryanuber
Copy link
Member

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

@ryanuber ryanuber added the type/enhancement Proposed improvement or new feature label May 16, 2015
@nicholascapo
Copy link

+1

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

@dpkirchner
Copy link

Any chance this will be merged in?

@slackpad slackpad self-assigned this Feb 23, 2016
@slackpad
Copy link
Contributor

@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.

@igrayson
Copy link

Looking forward to not having to use this workaround :)

@CpuID
Copy link

CpuID commented Sep 16, 2016

+1 would love to have this in :)

@slackpad bump :)

@asaldalop
Copy link

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

@stefreak
Copy link

stefreak commented Apr 19, 2017

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).

@nh2
Copy link

nh2 commented May 14, 2017

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

@stefreak
Copy link

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

@slackpad slackpad added the theme/cli Flags and documentation for the CLI interface label May 25, 2017
slackpad pushed a commit that referenced this issue Jul 27, 2017
* Exit 2 if -child-exit-code and the child returned with an error.
* There is no platform independent way to check the exact return code of
* the child, so on error always return 2.
* Closes #947
* Closes #1503
slackpad added a commit that referenced this issue Jul 27, 2017
* Exit 2 if -child-exit-code and the child returned with an error.
* There is no platform independent way to check the exact return code of
* the child, so on error always return 2.
* Closes #947
* Closes #1503
duckhan pushed a commit to duckhan/consul that referenced this issue Oct 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme/cli Flags and documentation for the CLI interface type/enhancement Proposed improvement or new feature
Projects
None yet
Development

No branches or pull requests

10 participants