Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trap cleanup 1 2 3 15
- foo=`cat`
- text='Éé' # UTF-8; shows up as Ãé on a latin1 terminal
- csi='␛['; dsr_cpr="${csi}6n"; dsr_ok="${csi}5n" # ␛ is an escape character
- stty_save=`stty -g`
- cleanup () { stty "$stty_save"; }
- trap 'cleanup; exit 120' 0 1 2 3 15 # cleanup code
- stty eol 0 eof n -echo # Input will end with `0n`
- # echo-n is a function that outputs its argument without a newline
- echo-n "$dsr_cpr$dsr_ok" # Ask the terminal to report the cursor position
- initial_report=`tr -dc ;0123456789` # Expect ␛[42;10R␛[0n for y=42,x=10
- echo-n "$text$dsr_cpr$dsr_ok"
- final_report=`tr -dc ;0123456789`
- cleanup
- # Compute and return initial_x - final_x
- foo=`{ { cat 1>&3; kill 0; } | { sleep 2; kill 0; } } 3>&1`
- #!/bin/sh
- foo=`sh -ic '{ cat 1>&3; kill 0; } | { sleep 2; kill 0; }' 3>&1 2>/dev/null`
- [ -n "$foo" ] && echo got: "$foo" || echo timeouted
- foo=`sh -ic 'exec 3>&1 2>/dev/null; { cat 1>&3; kill 0; } | { sleep 2; kill 0; }'`
- me=$$
- (sleep 2; kill $me >/dev/null 2>&1) & nuker=$!
- # do whatever
- kill $nuker >/dev/null 2>&1
- #!/bin/sh
- # Execute a command with a timeout
- # Author:
- # http://www.pixelbeat.org/
- # Notes:
- # Note there is a timeout command packaged with coreutils since v7.0
- # If the timeout occurs the exit status is 124.
- # There is an asynchronous (and buggy) equivalent of this
- # script packaged with bash (under /usr/share/doc/ in my distro),
- # which I only noticed after writing this.
- # I noticed later again that there is a C equivalent of this packaged
- # with satan by Wietse Venema, and copied to forensics by Dan Farmer.
- # Changes:
- # V1.0, Nov 3 2006, Initial release
- # V1.1, Nov 20 2007, Brad Greenlee <brad@footle.org>
- # Make more portable by using the 'CHLD'
- # signal spec rather than 17.
- # V1.3, Oct 29 2009, Ján Sáreník <jasan@x31.com>
- # Even though this runs under dash,ksh etc.
- # it doesn't actually timeout. So enforce bash for now.
- # Also change exit on timeout from 128 to 124
- # to match coreutils.
- # V2.0, Oct 30 2009, Ján Sáreník <jasan@x31.com>
- # Rewritten to cover compatibility with other
- # Bourne shell implementations (pdksh, dash)
- if [ "$#" -lt "2" ]; then
- echo "Usage: `basename $0` timeout_in_seconds command" >&2
- echo "Example: `basename $0` 2 sleep 3 || echo timeout" >&2
- exit 1
- fi
- cleanup()
- {
- trap - ALRM #reset handler to default
- kill -ALRM $a 2>/dev/null #stop timer subshell if running
- kill $! 2>/dev/null && #kill last job
- exit 124 #exit with 124 if it was running
- }
- watchit()
- {
- trap "cleanup" ALRM
- sleep $1& wait
- kill -ALRM $$
- }
- watchit $1& a=$! #start the timeout
- shift #first param was timeout for sleep
- trap "cleanup" ALRM INT #cleanup after timeout
- "$@" < /dev/tty & wait $!; RET=$? #start the job wait for it and save its return value
- kill -ALRM $a #send ALRM signal to watchit
- wait $a #wait for watchit to finish cleanup
- exit $RET #return the value
- $ nc -l 0 2345 | cat & # output come from here
- $ nc -w 5 0 2345 # input come from here and times out after 5 sec
- $ foo=`nc -l 0 2222 | nc -w 5 0 2222`
- #!/bin/sh
- stty -echo -onlcr
- # GNU script
- foo=`script -q -c 'sh -c "{ cat 1>&3; kill 0; } | { sleep 5; kill 0; }" 3>&1 2>/dev/null' /dev/null`
- # FreeBSD script
- #foo=`script -q /dev/null sh -c '{ cat 1>&3; kill 0; } | { sleep 5; kill 0; }' 3>&1 2>/dev/null`
- stty echo onlcr
- echo "foo: $foo"
- # alternative without: stty -echo -onlcr
- # cr=`printf 'r'`
- # foo=`script -q -c 'sh -c "{ { cat 1>&3; kill 0; } | { sleep 5; kill 0; } } 3>&1 2>/dev/null"' /dev/null | sed -e "s/${cr}$//" -ne 'p;N'` # GNU
- # foo=`script -q /dev/null sh -c '{ { cat 1>&3; kill 0; } | { sleep 5; kill 0; } } 3>&1 2>/dev/null' | sed -e "s/${cr}$//" -ne 'p;N'` # FreeBSD
- # echo "foo: $foo"
- eval 'set -m ; ( ... ) ; '"$(set +o)"
- f() { date ; kill 0 ; }
- echo Before
- eval 'set -m ; ( f ) ; '"$(set +o)"
- echo After
- $ sh /tmp/foo.sh
- Before
- Mon 14 Mar 2016 17:22:41 PDT
- /tmp/foo.sh: line 4: 17763 Terminated: 15 ( f )
- After
- timeout_handler () { echo Timeout. goodbye.; exit 1; }
- trap timeout_handler ALRM
- ( sleep 60; kill -ALRM $$ ) &
Add Comment
Please, Sign In to add comment