-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Change logic of stdin input to be last and only if required #2822
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
!checkflags | ||
!continueyn | ||
!verify-go-fmt.sh | ||
!time-out |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#!/bin/bash | ||
# | ||
# The Bash shell script executes a command with a time-out. | ||
# Upon time-out expiration SIGTERM (15) is sent to the process. If the signal | ||
# is blocked, then the subsequent SIGKILL (9) terminates it. | ||
# | ||
# Based on the Bash documentation example. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Kubuxu did you write this? if not, what is the license? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is Public Domain, I had previously credited the original author but removed it with other comments as for @RichardLitt 's request. I shouldn't have removed the author line, it was a signature for a short letter there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. licensing and author lines need to be back before release. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should address the license issue: #3152 |
||
|
||
scriptName="${0##*/}" | ||
|
||
declare -i DEFAULT_TIMEOUT=9 | ||
declare -i DEFAULT_INTERVAL=1 | ||
declare -i DEFAULT_DELAY=1 | ||
|
||
# Timeout. | ||
declare -i timeout=DEFAULT_TIMEOUT | ||
# Interval between checks if the process is still alive. | ||
declare -i interval=DEFAULT_INTERVAL | ||
# Delay between posting the SIGTERM signal and destroying the process by SIGKILL. | ||
declare -i delay=DEFAULT_DELAY | ||
|
||
function printUsage() { | ||
cat <<EOF | ||
|
||
Synopsis | ||
$scriptName [-t timeout] [-i interval] [-d delay] command | ||
Execute a command with a time-out. | ||
Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM | ||
signal is blocked, then the subsequent SIGKILL (9) terminates it. | ||
|
||
-t timeout | ||
Number of seconds to wait for command completion. | ||
Default value: $DEFAULT_TIMEOUT seconds. | ||
|
||
-i interval | ||
Interval between checks if the process is still alive. | ||
Positive integer, default value: $DEFAULT_INTERVAL seconds. | ||
|
||
-d delay | ||
Delay between posting the SIGTERM signal and destroying the | ||
process by SIGKILL. Default value: $DEFAULT_DELAY seconds. | ||
|
||
As of today, Bash does not support floating point arithmetic (sleep does), | ||
therefore all delay/time values must be integers. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have go-sleep that accepts floating point delays. |
||
EOF | ||
} | ||
|
||
# Options. | ||
while getopts ":t:i:d:" option; do | ||
case "$option" in | ||
t) timeout=$OPTARG ;; | ||
i) interval=$OPTARG ;; | ||
d) delay=$OPTARG ;; | ||
*) printUsage; exit 1 ;; | ||
esac | ||
done | ||
shift $((OPTIND - 1)) | ||
|
||
# $# should be at least 1 (the command to execute), however it may be strictly | ||
# greater than 1 if the command itself has options. | ||
if (($# == 0 || interval <= 0)); then | ||
printUsage | ||
exit 1 | ||
fi | ||
|
||
# kill -0 pid Exit code indicates if a signal may be sent to $pid process. | ||
( | ||
((t = timeout)) | ||
|
||
while ((t > 0)); do | ||
sleep $interval | ||
kill -0 $$ || exit 0 | ||
((t -= interval)) | ||
done | ||
|
||
# Be nice, post SIGTERM first. | ||
# The 'exit 0' below will be executed if any preceeding command fails. | ||
kill -s SIGTERM $$ && kill -0 $$ || exit 0 | ||
sleep $delay | ||
kill -s SIGKILL $$ | ||
) 2> /dev/null & | ||
|
||
exec "$@" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/sh | ||
# | ||
# Copyright (c) 2014 Christian Couder | ||
# MIT Licensed; see the LICENSE file in this repository. | ||
# | ||
|
||
test_description="Test init command with default config" | ||
|
||
. lib/test-lib.sh | ||
|
||
cfg_key="Addresses.API" | ||
cfg_val="/ip4/0.0.0.0/tcp/5001" | ||
|
||
# test that init succeeds | ||
test_expect_success "ipfs init succeeds" ' | ||
export IPFS_PATH="$(pwd)/.ipfs" && | ||
echo "IPFS_PATH: \"$IPFS_PATH\"" && | ||
BITS="2048" && | ||
ipfs init --bits="$BITS" >actual_init || | ||
test_fsh cat actual_init | ||
' | ||
|
||
test_expect_success ".ipfs/config has been created" ' | ||
test -f "$IPFS_PATH"/config || | ||
test_fsh ls -al .ipfs | ||
' | ||
|
||
test_expect_success "ipfs config succeeds" ' | ||
ipfs config $cfg_flags "$cfg_key" "$cfg_val" | ||
' | ||
|
||
test_expect_success "ipfs read config succeeds" ' | ||
IPFS_DEFAULT_CONFIG=$(cat "$IPFS_PATH"/config) | ||
' | ||
|
||
test_expect_success "clean up ipfs dir" ' | ||
rm -rf "$IPFS_PATH" | ||
' | ||
|
||
test_expect_success "ipfs init default config succeeds" ' | ||
echo $IPFS_DEFAULT_CONFIG | ipfs init - >actual_init || | ||
test_fsh cat actual_init | ||
' | ||
|
||
test_expect_success "ipfs config output looks good" ' | ||
echo "$cfg_val" >expected && | ||
ipfs config "$cfg_key" >actual && | ||
test_cmp expected actual | ||
' | ||
|
||
test_launch_ipfs_daemon | ||
|
||
test_kill_ipfs_daemon | ||
|
||
test_done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit concerned that this script is written in bash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chriscool we removed this script in #3152
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I saw it afterwards. Thanks for having written something more portable!