Guest User

Untitled

a guest
Feb 22nd, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. trap cleanup 1 2 3 15
  2. foo=`cat`
  3.  
  4. text='Éé' # UTF-8; shows up as Ãé on a latin1 terminal
  5. csi='␛['; dsr_cpr="${csi}6n"; dsr_ok="${csi}5n" # ␛ is an escape character
  6. stty_save=`stty -g`
  7. cleanup () { stty "$stty_save"; }
  8. trap 'cleanup; exit 120' 0 1 2 3 15 # cleanup code
  9. stty eol 0 eof n -echo # Input will end with `0n`
  10. # echo-n is a function that outputs its argument without a newline
  11. echo-n "$dsr_cpr$dsr_ok" # Ask the terminal to report the cursor position
  12. initial_report=`tr -dc ;0123456789` # Expect ␛[42;10R␛[0n for y=42,x=10
  13. echo-n "$text$dsr_cpr$dsr_ok"
  14. final_report=`tr -dc ;0123456789`
  15. cleanup
  16. # Compute and return initial_x - final_x
  17.  
  18. foo=`{ { cat 1>&3; kill 0; } | { sleep 2; kill 0; } } 3>&1`
  19.  
  20. #!/bin/sh
  21. foo=`sh -ic '{ cat 1>&3; kill 0; } | { sleep 2; kill 0; }' 3>&1 2>/dev/null`
  22. [ -n "$foo" ] && echo got: "$foo" || echo timeouted
  23.  
  24. foo=`sh -ic 'exec 3>&1 2>/dev/null; { cat 1>&3; kill 0; } | { sleep 2; kill 0; }'`
  25.  
  26. me=$$
  27. (sleep 2; kill $me >/dev/null 2>&1) & nuker=$!
  28. # do whatever
  29. kill $nuker >/dev/null 2>&1
  30.  
  31. #!/bin/sh
  32.  
  33. # Execute a command with a timeout
  34.  
  35. # Author:
  36. # http://www.pixelbeat.org/
  37. # Notes:
  38. # Note there is a timeout command packaged with coreutils since v7.0
  39. # If the timeout occurs the exit status is 124.
  40. # There is an asynchronous (and buggy) equivalent of this
  41. # script packaged with bash (under /usr/share/doc/ in my distro),
  42. # which I only noticed after writing this.
  43. # I noticed later again that there is a C equivalent of this packaged
  44. # with satan by Wietse Venema, and copied to forensics by Dan Farmer.
  45. # Changes:
  46. # V1.0, Nov 3 2006, Initial release
  47. # V1.1, Nov 20 2007, Brad Greenlee <brad@footle.org>
  48. # Make more portable by using the 'CHLD'
  49. # signal spec rather than 17.
  50. # V1.3, Oct 29 2009, Ján Sáreník <jasan@x31.com>
  51. # Even though this runs under dash,ksh etc.
  52. # it doesn't actually timeout. So enforce bash for now.
  53. # Also change exit on timeout from 128 to 124
  54. # to match coreutils.
  55. # V2.0, Oct 30 2009, Ján Sáreník <jasan@x31.com>
  56. # Rewritten to cover compatibility with other
  57. # Bourne shell implementations (pdksh, dash)
  58.  
  59. if [ "$#" -lt "2" ]; then
  60. echo "Usage: `basename $0` timeout_in_seconds command" >&2
  61. echo "Example: `basename $0` 2 sleep 3 || echo timeout" >&2
  62. exit 1
  63. fi
  64.  
  65. cleanup()
  66. {
  67. trap - ALRM #reset handler to default
  68. kill -ALRM $a 2>/dev/null #stop timer subshell if running
  69. kill $! 2>/dev/null && #kill last job
  70. exit 124 #exit with 124 if it was running
  71. }
  72.  
  73. watchit()
  74. {
  75. trap "cleanup" ALRM
  76. sleep $1& wait
  77. kill -ALRM $$
  78. }
  79.  
  80. watchit $1& a=$! #start the timeout
  81. shift #first param was timeout for sleep
  82. trap "cleanup" ALRM INT #cleanup after timeout
  83. "$@" < /dev/tty & wait $!; RET=$? #start the job wait for it and save its return value
  84. kill -ALRM $a #send ALRM signal to watchit
  85. wait $a #wait for watchit to finish cleanup
  86. exit $RET #return the value
  87.  
  88. $ nc -l 0 2345 | cat & # output come from here
  89. $ nc -w 5 0 2345 # input come from here and times out after 5 sec
  90.  
  91. $ foo=`nc -l 0 2222 | nc -w 5 0 2222`
  92.  
  93. #!/bin/sh
  94. stty -echo -onlcr
  95. # GNU script
  96. foo=`script -q -c 'sh -c "{ cat 1>&3; kill 0; } | { sleep 5; kill 0; }" 3>&1 2>/dev/null' /dev/null`
  97. # FreeBSD script
  98. #foo=`script -q /dev/null sh -c '{ cat 1>&3; kill 0; } | { sleep 5; kill 0; }' 3>&1 2>/dev/null`
  99. stty echo onlcr
  100. echo "foo: $foo"
  101.  
  102.  
  103. # alternative without: stty -echo -onlcr
  104. # cr=`printf 'r'`
  105. # 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
  106. # 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
  107. # echo "foo: $foo"
  108.  
  109. eval 'set -m ; ( ... ) ; '"$(set +o)"
  110.  
  111. f() { date ; kill 0 ; }
  112.  
  113. echo Before
  114. eval 'set -m ; ( f ) ; '"$(set +o)"
  115. echo After
  116.  
  117. $ sh /tmp/foo.sh
  118. Before
  119. Mon 14 Mar 2016 17:22:41 PDT
  120. /tmp/foo.sh: line 4: 17763 Terminated: 15 ( f )
  121. After
  122.  
  123. timeout_handler () { echo Timeout. goodbye.; exit 1; }
  124. trap timeout_handler ALRM
  125. ( sleep 60; kill -ALRM $$ ) &
Add Comment
Please, Sign In to add comment