make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / queue-mgmt / qadd
blobf6d8a08d3627552e9355dee345dd196c8f96a194
1 #!/bin/bash
3 usage_help_text="Usage: qadd [<OPTIONS>] [--] [<COMMAND> <ARGS>]
4 Add new task to the end of the queue.
5 A task is a command to run with its environment.
6 Qadd tries to preserve as much from the caller's environment as it can,
7 including environment variables, exported bash functions, working directoty, umask.
8 OPTIONS:
9 -q, --quiet
10 -d, --queue-dir <DIR>
11 -x, --if-not-queued
12 -X, --if-not-running"
14 qadd_no_dup=''
15 qadd_no_duprun=''
17 . qadd-common
20 if [ "$qadd_no_dup" -o "$qadd_no_duprun" ]
21 then
22 declare -a this_command=("$@")
24 shopt -s nullglob
25 for itemfile in "$queue_dir"/*.comm
27 argnum=0
28 same_command=1
30 exec {fd}<"$itemfile"
31 while read -r -d $'\0' -u $fd arg
33 if [ $argnum -ge ${#this_command[@]} ]
34 then
35 same_command=''
36 break
38 if [ ".$arg" != ".${this_command[$argnum]}" ]
39 then
40 same_command=''
41 break
43 argnum=$[argnum + 1]
44 done
45 exec {fd}>&-
46 if [ $same_command ] && [ $argnum != ${#this_command[@]} ]
47 then
48 same_command=''
51 if [ $same_command ]
52 then
53 taskid=`basename "$itemfile" .comm`
54 state=`qtask_state "$taskid"`
55 if [ \( "$qadd_no_duprun" -a "$state" = running \) -o \( "$qadd_no_dup" -a "$state" = queued \) ]
56 then
57 echo "qadd: not adding, command is equivalent to task $taskid" >&2
58 exit -8
61 done
62 shopt -u nullglob
65 queue_item_file=''
66 last_id=`cat "$queue_dir/.seq" 2>/dev/null || echo -n 0`
68 while [ -z "$queue_item_file" -o -e "$queue_item_file" ]
70 item_id=$[last_id + 1]
71 queue_item_file=$queue_dir/$item_id.comm
72 last_id=$item_id
73 done
76 pwd > "$queue_dir/$item_id.pwd"
77 umask > "$queue_dir/$item_id.umask"
79 for arg in "$@"
81 printf '%s\000' "$arg"
82 done > "$queue_item_file"
84 declare -p -x > "$queue_dir/$item_id.env.bash"
85 func=`declare -p -x -f`
86 if [ -n "$func" ]
87 then
88 echo "$func" > "$queue_dir/$item_id.func.bash"
92 echo -n $item_id > "$queue_dir/.seq"
95 if [ ! $quiet ]
96 then
97 echo "qadd: task $item_id queued in $queue_dir" >&2