Guest User

Untitled

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # NAME
  4. # fal - wrapper script to run several concurrent commands and gather their
  5. # output into one log file. fal is an abbreviation for "fork, aggregate, log"
  6. #
  7. # SYNOPSIS
  8. # fal -c count [-l logfile] [-t] [-p pipe] [--] command [command_args]
  9. #
  10. # OPTIONS
  11. # -c count
  12. # Run command concurrently count times.
  13. #
  14. # -l logfile
  15. # Store logs to the logfile. By default logfile is stored in current
  16. # directory and called the same as the command to run but with ".log"
  17. # extension. If such file exists, the output will be attached to the end
  18. # of the file.
  19. #
  20. # -p pipe
  21. # Create named pipe for aggregating stdout of processes. By default file
  22. # is called command.pipe
  23. #
  24. # -t
  25. # write output to both logfile and stdout
  26. #
  27. # The command will be run in a subshell with its arguments. If for some reason
  28. # arguments are similar to those accepted by this script, use "--" to separate
  29. # fal arguments and command to be run.
  30.  
  31. print_usage()
  32. {
  33. echo "Usage: $0 -c count [-l logfile] [-p pipe] [--] command [command_args]" >&2
  34. exit 1;
  35. }
  36.  
  37. # Parsing arguments
  38. if [ "$#" -lt 3 ] ; then
  39. print_usage;
  40. fi
  41.  
  42. set -- `getopt "c:l:p:t" "$@"`
  43.  
  44. while [ ! -z "$1" ]
  45. do
  46. case $1 in
  47. -c) COUNT=$2 ;;
  48. -l) LOG_FILENAME=$2 ;;
  49. -p) PIPE_FILENAME=$2 ;;
  50. -t) ENABLE_STDOUT=1 ;;
  51. --) break ;;
  52. esac
  53.  
  54. shift
  55. done
  56.  
  57. # Check variables and set default settings if needed
  58. COMMAND_STRING=$@
  59. COMMAND_STRING=${COMMAND_STRING#*-- }
  60. COMMAND=${COMMAND_STRING%% *}
  61.  
  62. if [ -z "$COUNT" ] ; then
  63. print_usage;
  64. fi
  65.  
  66. if [ -z "$LOG_FILENAME" ] ; then
  67. LOG_FILENAME="$COMMAND.log"
  68. fi
  69.  
  70. if [ -z "$PIPE_FILENAME" ] ; then
  71. PIPE_FILENAME="$COMMAND.pipe"
  72. fi
  73.  
  74. # Some debugging info before actual work.
  75. #echo "ARGV = $@"
  76. #echo "COMMAND_STRING = $COMMAND_STRING"
  77. #echo "COMMAND = $COMMAND"
  78. #echo "LOG_FILENAME = $LOG_FILENAME"
  79. #echo "PIPE_FILENAME = $PIPE_FILENAME"
  80. #echo "COUNT = $COUNT"
  81.  
  82. if [ ! -p $PIPE_FILENAME ] ; then
  83. mkfifo $PIPE_FILENAME
  84. fi
  85.  
  86. for ((i=1 ; i <= COUNT ; i++ ))
  87. do
  88. eval $COMMAND_STRING >$PIPE_FILENAME 2>$PIPE_FILENAME &
  89. done
  90.  
  91. # The next command will never exit until all PIDs who feed the pipe exit. After,
  92. # command will exit with "broken pipe" message which will be suppressed.
  93. if [ -z "$ENABLE_STDOUT" ] ; then
  94. cat $PIPE_FILENAME >> $LOG_FILENAME 2>/dev/null
  95. else
  96. cat $PIPE_FILENAME | tee $LOG_FILENAME
  97. fi
  98.  
  99. # Cleaning up before exit
  100. rm $PIPE_FILENAME
  101.  
  102. exit 0;
Add Comment
Please, Sign In to add comment