add some boilerplate scripts
[hband-tools.git] / queue-mgmt / qls
blob3560e2ba94183ae542be4656d02874f4908c16f9
1 #!/bin/bash
3 usage_help_text="qls [<OPTIONS>] [<ID>]
4 List items in the queue, or show a particular task's details if <ID> is given.
5 Fields:
6 ID task unique id
7 ADDED when the task was added
8 STATE queued, running, ended, ended(<CODE>), gone
9 COMMAND command to run, shown either in multiple lines (2nd and following lines prepended by TABs)
10 or in a single line with backslash and newline chars escaped by backslash
11 OPTIONS:
12 -q, --quiet
13 --multiline (default)
14 -M, --no-multiline, --escape-command
15 -d, --queue-dir <DIR>
16 -H, --no-header
17 -h, --hide-state <STATE>
18 -e, --show-exit-code"
20 qls_multiline=yes
21 qls_header=yes
22 qls_show_exit_status=''
23 qls_hide_states=''
25 . qadd-common
28 if [ ! $quiet ]
29 then
30 echo "qls: $queue_dir" >&2
33 if [ $# -gt 0 ]
34 then
35 for item_id in "$@"
37 itemfile=$queue_dir/$item_id.comm
38 load_command_global "$itemfile"
39 echo -n "Command: "
40 declare -p command | cut -d= -f2-
42 state=`qtask_state "$item_id"`
43 echo "State: $state"
45 pidfile=$queue_dir/$item_id.pid
46 if [ -e "$pidfile" ]
47 then
48 pid=`cat "$pidfile" 2>/dev/null`
49 echo -n "Pid: $pid"
50 task_pidns=`cat "$queue_dir/$item_id.pidns" 2>/dev/null`
51 curr_pidns=`readlink /proc/self/ns/pid`
52 if [ "$task_pidns" != "$curr_pidns" ]; then echo " pidns=${task_pidns#*:}"; else echo; fi
54 timestamp=`stat -c %Y "$itemfile"`
55 datetime=`date +"%F %T" -d @$timestamp`
56 echo "Added: $datetime"
58 timestamp=`stat -c %Y "$pidfile"`
59 datetime=`date +"%F %T" -d @$timestamp`
60 echo "Started: $datetime"
62 if [ -d /proc/$pid ]
63 then
64 load_command_global "/proc/$pid/cmdline"
65 echo -n "Process: "
66 declare -p command | cut -d= -f2-
67 process_status=`grep "^State:" /proc/$pid/status 2>/dev/null | sed -e 's/^[^:]\+:\s*//'`
68 echo "Process Status: $process_status"
72 endfile=$queue_dir/$item_id.end
73 if [ -e "$endfile" ]
74 then
75 timestamp=`stat -c %Y "$endfile"`
76 datetime=`date +"%F %T" -d @$timestamp`
77 echo "Ended: $datetime"
79 code=`cat "$endfile"`
80 echo "Returned: $code"
82 done
84 exit
87 if [ $qls_header ]
88 then
89 echo "ID ADDED STATE COMMAND"
92 shopt -s nullglob
94 for itemfile in "$queue_dir"/?.comm "$queue_dir"/??.comm "$queue_dir"/???.comm "$queue_dir"/????.comm "$queue_dir"/?????.comm "$queue_dir"/??????.comm "$queue_dir"/???????.comm
96 item_id=`basename "$itemfile" .comm`
97 state=`qtask_state "$item_id"`
98 state_shown=$state
100 if grep -qw "$state" <<< "$qls_hide_states"
101 then
102 continue
105 timestamp=`stat -c %Y "$itemfile"`
106 datetime=`date +"%F %T" -d @$timestamp`
107 load_command_global "$itemfile"
108 command_str=${command[*]}
110 if [ $qls_show_exit_status ]
111 then
112 if [ "$state" = ended ]
113 then
114 code=`cat "$queue_dir/$item_id.end"`
115 code=${code:-NA}
116 state_shown="$state($code)"
120 if [ $qls_multiline ]
121 then
122 command_shown=${command_str//$'\n'/$'\n\t\t\t'}
123 else
124 command_shown=${command_str//\\/\\\\}
125 command_shown=${command_shown//$'\n'/\\n}
128 echo "$item_id $datetime $state_shown $command_shown"
129 done