Repository navigation
Expand file tree
/
Copy pathxjar
More file actions
924 lines (807 loc) · 42.5 KB
/
Copy pathxjar
File metadata and controls
924 lines (807 loc) · 42.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
#!/bin/bash
# Copyright 2021 bin jin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Unset functions.
unset $( set | awk '/^__xj_[0-9a-z_]+[[:space:]]\(/{printf " " $1}');
__xj_run () {
# TODO: XJ_JAR_ARGS should support "".
if [ "$XJ_MAIN_CLASS_PATH" ]; then
set -- java $XJ_JAVA_ARGS -cp "$XJ_JAR_FILE" $XJ_MAIN_CLASS_PATH $XJ_JAR_ARGS;
else
set -- java $XJ_JAVA_ARGS -jar "$XJ_JAR_FILE" $XJ_JAR_ARGS;
fi
[ -e "${XJ_JAVA_BIN_DIR%/}/java" ] && export PATH=${XJ_JAVA_BIN_DIR%/}:$PATH;
[ -x $XJ_MINUS_1_ARG ] && set -- $XJ_MINUS_1_ARG "$@";
(
export SHELL=/sbin/nologin;
readonly SHELL;
if [ "$xj_jar_log_prefix" ]; then
{
# Unset environment variables and functions.
unset $( set | awk -F '[=[:space:]]' '/^(__)?(XJ|xj)_[0-9A-Za-z_]+(=|[[:space:]]\()/{printf " " $1}');
nohup < /dev/null "$@"
} 2>&1 | awk '{print '"$xj_jar_log_prefix"';fflush(stdout)}' &
else
unset $( set | awk -F '[=[:space:]]' '/^(__)?(XJ|xj)_[0-9A-Za-z_]+(=|[[:space:]]\()/{printf " " $1}');
nohup < /dev/null "$@" 2>&1 &
fi
)
exit 0
}
__xj_test_zip_head() {
local xj_file_head xj_zip_head;
printf -v xj_zip_head PK\\x03\\x04;
read xj_file_head 2>/dev/null < "$1";
[ "$xj_zip_head" == "${xj_file_head:0:4}" ] || {
printf "[${XJ_B_RED}ERROR${XJ_RE}] '$1' is not a jar file.\n" >&2;
return 1
};
return 0
}
[ "$XJ_JAR_FILE" ] && {
__xj_test_zip_head "$XJ_JAR_FILE" || exit 1;
__xj_run;
exit
}
__xj_out2() {
XJ_OUT2=tty
local fd;
for fd in /proc/$$/fd/*; do
[ ${fd##*/} == 0 -o "$fd" -ef "$0" ] && continue;
if [ -f "$fd" ]; then
XJ_OUT2=file;
break
elif [ -p "$fd" ]; then
XJ_OUT2=pipe;
break
fi
done
}
__xj_permission() {
[ $UID == 0 ] || {
printf "[${XJ_B_RED}ERROR${XJ_RE}] Permission denied; try the '${XJ_GREEN}sudo %s${XJ_RE}' command.\n" "${0##*/} $@" >&2;
exit 1
}
}
__xj_usage_help() {
printf "
xjar - JAR application process manager and operations tool
Usage: ${0##*/} [param...] ${XJ_BOLD}[option]${XJ_RE} [args...]
Parameters:
-d | --dir path [path...] ${XJ_BOLD}[option]${XJ_RE} run in batch over the given paths.
-h | --help print full help.
${XJ_BOLD}Options${XJ_RE}:
start [-D|--debug [port]] start the jar if it is not running (supports debug).
[--live] restart the jar if it is not running,
and log live every 10 minutes.
stop [-f] stop the jar process (force stop).
log [[lines] -[days]] print the last N lines from the last N days (default $XJ_LOG_PRINT_LAST_LINES lines).
e.g.,
${0##*/} log 100 -0 # the last 100 lines of today.
${0##*/} log 500 -0 | grep -B 5 -A 15 -i error # search the error log.
[--rotate] split the running log without restarting the jar:
move the log written so far into the previous day's
file and keep appending the new log to today's file.
e.g.,
${0##*/} log --rotate
${0##*/} log --rotate --strict # freeze the writer
briefly so no log line is ever lost.
(--redirect is kept as an alias of --rotate.)
[--cron [path]] automatically rotate the log file every day
(random start minute and random delay).
e.g.,
${0##*/} log --cron /var/run
[--rm [days]] delete log files modified more than N days ago (print the command only).
e.g.,
${0##*/} log --rm 30 | bash
restart [-D|--debug [port]] restart the jar process (supports debug).
status [-v] show process info (and thread info).
exec: thread_name.
flt: major_page_faults+minor_page_faults.
kfun: kernel_function.
lsn: listen_port/protocol(established_count/link_count).
send: protocol(link_count/ip_port_count).
thrd: thread_count.
time: days-hours:minute.
u: user.
e.g.,
${0##*/} status -v | sort -k 5
info [--help|-h] show the settings (config examples).
Examples:
xjar -d .
xjar start -D 8081
xjar -d /var/run restart -D 8081
xjar stop -f
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user@hostname 'export PATH=\$PATH:\$HOME/bin; xjar -d \$JAR_DIR1 \$JAR_DIR2 restart'
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no user@hostname 'export PATH=\$PATH:\$HOME/bin; cd \$JAR_DIR; xjar restart; sleep 30; xjar || { xjar log -0 100; exit 1; }'
" >&2
}
__xj_conf_help() {
[ "$UID" == "0" ] || printf "
visudo:
cat >> ${XJ_GREEN}/etc/sudoers${XJ_RE} <<-EOF${XJ_REVERSE}
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
# Cmnd alias specification
Cmnd_Alias JAR_RUNNER = $0
# User privilege specification
$USER ALL=(ALL) NOPASSWD: JAR_RUNNER
${XJ_RE}EOF
" >&2;
printf "
Global config:
cat > ${XJ_GREEN}/etc/xjar.conf${XJ_RE} <<-EOF${XJ_REVERSE}
LANG=zh_CN.UTF-8;
LC_ALL=zh_CN.UTF-8;
FIND_JAR_MAX_DEPTH=2;
# LOG_PRINT_LAST_LINES=69
# LOCAL_XJAR_CONFIG_NAME=.xjarconf
# MINUS_1_ARG=./run.sh
JAVA_ARGS=' -Xmx1024m'
JAVA_ARGS+=' -Xms1024m'
${XJ_RE}EOF
User config:
cat > ${XJ_GREEN}$HOME/.xjarc${XJ_RE} <<-EOF${XJ_REVERSE}
JAR_LOG_DIR_PRE=/var/log/java;
JAR_LOG_DIR_SUF=/console;
LIVE_LOG_DIR=/var/log/xjar-live;
JAR_ARGS=' --config.location=./application.yml'
# JAVA_BIN_DIR=/usr/lib/jvm/openjdk-8/bin
# LOG_ROTATE_CHUNK=4194304; # Bytes moved per log rotation step.
# LOG_ROTATE_DELAY=1800; # Max random delay (seconds) of the daily rotation.
# LOG_ROTATE_STRICT=1; # Freeze the writer briefly so no log line is lost.
STOP_TIMEOUT=30
${XJ_RE}EOF
Local config:
cat > ${XJ_GREEN}$XJ_LOCAL_XJAR_CONFIG_NAME${XJ_RE} <<-EOF${XJ_REVERSE}
# JAR_LOG_DIR=/var/log/java/xxx/console; # Deprecated
# LOG_PREFIX='%%F %%T,'
DEBUG_PORT=9000;
# MAIN_CLASS_PATH='java.util.prefs.Base64'
JAR_ARGS=' --prefix=/xxx'
${XJ_RE}EOF
" >&2
}
# Find the awk process that runs 'strftime'.
__xj_pipe_pid () {
[ "$1" ] || return 1;
local xj_awk_pid xj_awk_pipe xj_fd_1="$(readlink --canonicalize /proc/$1/fd/1)";
if [ "${xj_fd_1/\/pipe:\[/}" != "$xj_fd_1" ]; then
while read xj_awk_pid xj_awk_pipe; do
[ "${xj_fd_1%"$xj_awk_pipe"}" == "$xj_fd_1" ] || {
printf $xj_awk_pid;
return 0
};
done < <(ps -eo pid,cmd | awk '/strftime\(/{system("readlink --canonicalize /proc/" $1 "/fd/0")}' | awk -F '/' '{print $3, $5}')
# If no awk process is found by pipe, override stdout and stderr with a text file.
fi
return 1
}
# The pid that owns the log file: the awk prefix process if LOG_PREFIX is set,
# otherwise the java process itself.
__xj_log_writer_pid() {
__xj_pipe_pid "$1" || printf '%s' "$1"
}
# True when the file of the given pid is opened with O_APPEND. The rotation
# only ever cuts the file tail, so an appending writer keeps working.
__xj_log_is_append() {
local xj_flags;
xj_flags=$(awk '/^flags:/{print $2}' /proc/$1/fdinfo/1 2>/dev/null);
case "$xj_flags" in ''|*[!0-7]*) return 1;; esac;
[ $(( 0$xj_flags & 02000 )) -ne 0 ]
}
# Resume a writer that strict mode froze.
__xj_log_resume() {
[ "$1" ] && kill -CONT "$1" 2>/dev/null;
return 0
}
# Split $1 (live log) so that everything written before this run ends up in $2
# (archive) and the live file keeps only the newer log. $3 is a scratch file,
# $4 the owner and $5 the writer pid (only set in strict mode).
#
# The source is walked from its tail towards its head: each step copies one
# block into the archive at the very same offset, then cuts that block off the
# source tail. The archive is pre-sized as a sparse file, so the space it gains
# is exactly the space the source releases: disk usage stays flat and no
# special filesystem support is needed. The log written after the boundary is
# carried down with every step, so it is preserved; only bytes appended during
# the final truncate of a step can be lost.
#
# When $5 is set (strict mode) the writer is frozen with SIGSTOP for that short
# critical section only, so nothing can be appended there and no byte is lost.
# The chunk copy itself runs with the writer running, keeping the freeze at
# microseconds-to-milliseconds per step.
__xj_log_rotate() {
local xj_live="$1" xj_arch="$2" xj_spool="$3" xj_owner="$4" xj_wpid="$5";
local xj_bs=4096 xj_chunk xj_size xj_hi xj_lo xj_b xj_nstart xj_nlen xj_rem;
[ -s "$xj_live" ] || return 0;
xj_chunk=${XJ_LOG_ROTATE_CHUNK:-4194304};
case "$xj_chunk" in ''|*[!0-9]*|0) xj_chunk=4194304;; esac;
[ "$xj_chunk" -ge "$xj_bs" ] || xj_chunk=$xj_bs;
xj_chunk=$(( xj_chunk / xj_bs * xj_bs ));
xj_size=$(stat -c %s "$xj_live") || return 1;
xj_hi=$(( xj_size / xj_bs * xj_bs ));
xj_nstart=$xj_size;
case "$xj_wpid" in ''|*[!0-9]*) xj_wpid=;; esac;
# Never leave the writer frozen if this run is killed mid-step.
[ "$xj_wpid" ] && trap "kill -CONT $xj_wpid 2>/dev/null" EXIT INT TERM HUP;
mkdir -p "${xj_arch%/*}" || return 1;
: > "$xj_arch" || return 1;
# Pre-size the archive without using blocks; the tail is filled first.
truncate -s "$xj_size" "$xj_arch" || return 1;
chown "$xj_owner" "$xj_arch" 2>/dev/null;
: > "$xj_spool" || return 1;
# The sub-block tail of the old log.
xj_rem=$(( xj_size - xj_hi ));
if [ "$xj_rem" -gt 0 ]; then
dd if="$xj_live" of="$xj_arch" bs=1 skip="$xj_hi" seek="$xj_hi" count="$xj_rem" conv=notrunc status=none || return 1;
fi
while :; do
if [ "$xj_hi" -gt 0 ]; then
xj_b=$xj_chunk; [ "$xj_b" -gt "$xj_hi" ] && xj_b=$xj_hi;
xj_lo=$(( xj_hi - xj_b ));
dd if="$xj_live" of="$xj_arch" bs="$xj_bs" skip="$(( xj_lo / xj_bs ))" seek="$(( xj_lo / xj_bs ))" count="$(( xj_b / xj_bs ))" conv=notrunc status=none || return 1;
else
xj_lo=0;
fi
# Carry the log written after the boundary down with the head.
[ "$xj_wpid" ] && kill -STOP "$xj_wpid" 2>/dev/null;
tail -c +$(( xj_nstart + 1 )) "$xj_live" > "$xj_spool" || { __xj_log_resume "$xj_wpid"; return 1; };
xj_nlen=$(stat -c %s "$xj_spool");
if [ "$xj_nlen" -gt 0 ]; then
dd if="$xj_spool" of="$xj_live" bs="$xj_bs" seek="$(( xj_lo / xj_bs ))" count="$(( (xj_nlen + xj_bs - 1) / xj_bs ))" conv=notrunc status=none || { __xj_log_resume "$xj_wpid"; return 1; };
fi
truncate -s "$(( xj_lo + xj_nlen ))" "$xj_live" || { __xj_log_resume "$xj_wpid"; return 1; };
__xj_log_resume "$xj_wpid";
[ "$xj_lo" -eq 0 ] && break;
xj_hi=$xj_lo;
xj_nstart=$xj_lo;
done
[ "$xj_wpid" ] && trap - EXIT INT TERM HUP;
rm -f "$xj_spool";
return 0
}
__xj_test_command() {
which $1 >/dev/null 2>&1 && return 0;
printf "[${XJ_B_RED}ERROR${XJ_RE}] '%s' command ${XJ_RED}not${XJ_RE} found or ${XJ_RED}not${XJ_RE} in '\$PATH'\n" $1 >&2;
return 1;
}
__xj_test_workspace() {
[ -s "$XJ_JAR_FILE" ] && return 0;
printf "[${XJ_B_RED}ERROR${XJ_RE}] '${XJ_GREEN}%s${XJ_RE}' is not a working directory.\n" "$PWD" >&2;
exit 1
}
__xj_test_single_process() {
[ "${xj_java_pid/[^0-9]/}" == "$xj_java_pid" ] && return 0;
printf "name:${XJ_BOLD}%-26s${XJ_RE} [${XJ_B_RED}ERROR${XJ_RE}] not a single process. pid: ${XJ_BOLD}%s${XJ_RE}\n" $xj_jar_name, "${xj_java_pid//[^0-9]/ }" >&2;
exit 1
}
__xj_load_config() {
[ -s "$1" ] && eval $(awk -F \# '$1 ~ /=/{gsub(/^[[:space:]]+|[[:space:]]+$|;[[:space:]]*$/, "", $1); if(match($1, /^[^=]+="/)){sub(/="/, "=", $1); sub(/"$/, "", $1)}; gsub(/[`"\\]|\$\(/, "\\\\&", $1); printf "export XJ_"; if(match($1, /^[^=]+='\''/)) { print $1 } else print gensub(/=/, "&\"", 1, $1) "\""}' "$1");
}
__xj_java_args() {
if [ "$XJ_MAIN_CLASS_PATH" ]; then
set -- $XJ_JAVA_ARGS -cp "$XJ_JAR_FILE" $XJ_MAIN_CLASS_PATH $XJ_JAR_ARGS;
else
set -- $XJ_JAVA_ARGS -jar "$XJ_JAR_FILE" $XJ_JAR_ARGS;
fi
printf '%s' "$*"
}
__xj_main() {
# Unset environment variables.
unset CLASSPATH JAVA_HOME $( set | awk -F = '/^(XJ|xj)_[0-9A-Za-z_]+=/{printf " " $1}');
__xj_out2;
[ "$XJ_OUT2" == "tty" ] && {
# Console color codes.
XJ_RE='\x1b[0m';
XJ_BOLD='\033[1m';
XJ_B_RED='\e[1;31m';
XJ_RED='\e[31m';
XJ_GREEN='\e[32m';
XJ_BROWN='\e[33m';
XJ_BLUE='\e[34m';
XJ_REVERSE='\e[7m';
}
XJ_LOCAL_XJAR_CONFIG_NAME='.xjarconf';
XJ_LOG_PRINT_LAST_LINES=69;
XJ_FIND_JAR_MAX_DEPTH=2;
__xj_load_config /etc/xjar.conf;
__xj_load_config $HOME/.xjarc;
__xj_load_config $XJ_LOCAL_XJAR_CONFIG_NAME;
eval $( set | awk '{if($_ ~ /^{[[:space:]]|[[:space:]]\(\)[[:space:]]$/) exit; if($_ !~ /^(XJ_|_=)/ && $_ ~ /^[A-Z_]+=[^(][^*]{,128}$/){print "export XJ_" $0}}');
[ "$XJ_JAVA_BIN_DIR" ] && { [ ! -e "${XJ_JAVA_BIN_DIR%/}/java" ] && { printf "[${XJ_B_RED}ERROR${XJ_RE}] '${XJ_GREEN}%s${XJ_RE}' is not a Java bin directory.\n" "$XJ_JAVA_BIN_DIR" >&2; exit 1; }; export PATH=${XJ_JAVA_BIN_DIR%/}:$PATH; }
if [ "$XJ_JAR_LOG_DIR" ]; then
:
elif [ "$XJ_JAR_LOG_DIR_PRE" ]; then
XJ_JAR_LOG_DIR="${XJ_JAR_LOG_DIR_PRE%/}/${PWD##*/}/${XJ_JAR_LOG_DIR_SUF#/}";
else
if [ "$XJ_JAR_LOG_DIR_SUF" ]; then
XJ_JAR_LOG_DIR="/var/log/java/${PWD##*/}/${XJ_JAR_LOG_DIR_SUF#/}"
else
XJ_JAR_LOG_DIR="/var/log/java/${PWD##*/}/console"
fi
fi
XJ_JAR_LOG_DIR="${XJ_JAR_LOG_DIR%/}";
: ${XJ_LIVE_LOG_DIR:='/var/log/xjar-live'};
for XJ_JAR_FILE in "$PWD/"*.jar; do
[ -f "$XJ_JAR_FILE" ] && \
[ "$XJ_JAR_FILE$XJ_JAR_FILE" == \
"${XJ_JAR_FILE/[_-\.]'sources'[_-\.]/}${XJ_JAR_FILE/[_-\.]'src'[_-\.]/}" ] && \
__xj_test_zip_head "$XJ_JAR_FILE" && break;
unset XJ_JAR_FILE
done
XJ_JAR_FILE="${XJ_JAR_FILE/*\**/}"; # Unset XJ_JAR_FILE if the jar file was not found.
[ -s "$XJ_JAR_FILE" ] && {
xj_jar_name="${XJ_JAR_FILE##*/}";
xj_jar_name="${xj_jar_name%.*}";
# xj_args_file="$XJ_JAR_LOG_DIR/$xj_jar_name.args";
xj_jar_dir="${XJ_JAR_FILE%/*}/";
xj_user=`ls -l "$XJ_JAR_FILE" | awk '{if(NR == 1) printf $3}'`;
xj_group=$(id -gn ${xj_user:-0,} 2>/dev/null) || {
printf "[${XJ_B_RED}ERROR${XJ_RE}] the file '${XJ_GREEN}%s.jar${XJ_RE}' is owned by user '${XJ_GREEN}%s${XJ_RE}', which does not exist.\n" "$xj_jar_name" "$xj_user" >&2;
exit 1
}
xj_java_pid=$(ps -eo pid,cmd | awk '/^[[:space:]]*[0-9]+[[:space:]]+java[[:space:]].*'"${xj_jar_dir//\//\\/}"'.*\.jar/{print $1}');
[ "$XJ_LOG_PREFIX" ] && {
# See 'man date'.
[ "$XJ_LOG_PREFIX" == "${XJ_LOG_PREFIX/\%[A-DF-IMNPR-Za-eghj-npr-z]/}" -a "$XJ_LOG_PREFIX" == "${XJ_LOG_PREFIX/\%-[A-DF-IMNPR-Za-eghj-npr-z]/}" ] && {
printf "[${XJ_B_RED}ERROR${XJ_RE}] ${XJ_BOLD}LOG_PREFIX${XJ_RE} invalid option: '${XJ_BROWN}%s${XJ_RE}' in '${XJ_GREEN}%s${XJ_RE}'\n" "$XJ_LOG_PREFIX" "$XJ_LOCAL_XJAR_CONFIG_NAME" >&2;
exit 1
};
xj_jar_log_prefix='strftime("'"$XJ_LOG_PREFIX"'"), $0'; # For awk.
};
};
__xj_fd_file() { local xj_fd; readlink `for xj_fd in /proc/$1/fd/*; do [ -f $xj_fd ] && printf " %s" $xj_fd; done`; }
__xj_colorless() { [ "$XJ_OUT2" != "tty" ] && { sed --unbuffered --regexp-extended 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]|\r//g'; :; } || cat; }
case $1 in
start)
__xj_permission "$@";
__xj_test_command java || exit 1;
__xj_test_workspace;
[ "$2" == "--live" ] && {
mkdir -p "$XJ_LIVE_LOG_DIR";
if [ "$xj_java_pid" -a "${xj_java_pid/[^0-9]/}" == "$xj_java_pid" ]; then
# Print every 10 minutes.
[ $((1`date +%M` % 10)) == 0 ] && printf "%s\n" live
else
printf "restart: ";
free -h | awk '/Mem:/{printf "u/a: %s/%s\n", $3, $2}';
export TEAMCITY_VERSION=none TERM=dumb;
nohup < /dev/null bash "$0" restart >/dev/null 2>&1 &
fi
} 2>&1 | awk -v dir_prefix="$XJ_LIVE_LOG_DIR" -v arg_name=$xj_jar_name \
'{printf "%s, %-26s %s\n" ,strftime("%F %T"), arg_name ",", $0 >> strftime(dir_prefix "/live_%Y%m%d.log");fflush(stdout)}' && \
exit 0
__xj_test_command readlink || :;
__xj_test_single_process;
[ "$xj_java_pid" ] && { printf "name:${XJ_BOLD}%-26s${XJ_RE} is running.\n" $xj_jar_name; exit 1; } >&2;
shift;
case $1 in
"") :;;
--debug|-D)
[ "$2" ] && xj_port=$2 || xj_port=$XJ_DEBUG_PORT;
[ "$xj_port" ] || {
printf "[${XJ_B_RED}ERROR${XJ_RE}] DEBUG_PORT ${XJ_RED}not${XJ_RE} set\n" >&2;
exit 1
};
xj_java_main_version=$(java -version 2>&1 | awk -F '[[:space:]".]' '/version/{print $4 * 1000 + $5}');
if [ $xj_java_main_version -lt 1005 ]; then
XJ_JAVA_ARGS+=" -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$xj_port";
elif [ $xj_java_main_version -gt 1008 ]; then
XJ_JAVA_ARGS+=" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:$xj_port";
else
XJ_JAVA_ARGS+=" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$xj_port";
fi
# jdb -connect com.sun.jdi.SocketAttach:hostname=127.0.0.1,port=$xj_port
unset xj_java_main_version xj_port
;;
*)
printf "[${XJ_B_RED}ERROR${XJ_RE}] Invalid option: start '${XJ_BROWN}%s${XJ_RE}'\n" "$1" >&2;
__xj_usage_help;
exit 1
;;
esac
mkdir -pv "$XJ_JAR_LOG_DIR";
chown -R $xj_user:$xj_group "$XJ_JAR_LOG_DIR";
# echo "$@" > "$xj_args_file" && chown $xj_user:$xj_group "$xj_args_file";
xj_jar_log_file="$(date +$XJ_JAR_LOG_DIR/${xj_jar_name}_%Y%m%d.log)";
if [ "$XJ_MINUS_1_ARG" -a -s "$XJ_MINUS_1_ARG" ]; then
[ -x "$XJ_MINUS_1_ARG" ] || {
printf "[${XJ_B_RED}ERROR${XJ_RE}] '${XJ_GREEN}%s${XJ_RE}' does not have executable permissions.\n" "$XJ_MINUS_1_ARG" >&2;
exit 1
};
fi
export XJ_JAVA_BIN_DIR XJ_JAR_FILE xj_jar_log_prefix XJ_JAVA_ARGS XJ_JAR_ARGS XJ_MAIN_CLASS_PATH XJ_MINUS_1_ARG;
[ "$XJ_LANG" ] && export LANG=$XJ_LANG;
[ "$XJ_LC_ALL" ] && export LC_ALL=$XJ_LC_ALL;
touch "$xj_jar_log_file";
chown $xj_user:$xj_group "$xj_jar_log_file";
su --shell $SHELL $xj_user --command "$0" >> "$xj_jar_log_file" || exit 1;
[ "$TERM" != "dumb" -a ! "$TEAMCITY_VERSION" ] && {
printf "nohup < /dev/null ${XJ_BLUE}java %s${XJ_RE} >> ${XJ_GREEN}%s${XJ_RE} 2>&1 &\n" "$(__xj_java_args)" "$xj_jar_log_file";
tail -F -n 0 "$xj_jar_log_file" 2>/dev/null | __xj_colorless
}
;;
log)
[ "$2" == "--cron" ] || __xj_test_workspace;
case $2 in
""|[0-9]*|-[0-9]*)
shift;
[ "$1" == "${1/-[0-9]/}" ] || set -- ${2:-$XJ_LOG_PRINT_LAST_LINES} $1;
xj_days=${2:-0};
if [ "$TERM" != "dumb" -a ! "$TEAMCITY_VERSION" ] && [ ! "$2" ]; then
if [ "$xj_java_pid" ]; then
eval set -- ${1:-$XJ_LOG_PRINT_LAST_LINES} $(
__xj_fd_file $xj_java_pid | awk '/(LOG|log)$/{if(!unique[$0]++) line[++n]=$_}END{for(i=length(line); i>0; i--) printf " \"%s\"", line[i]}'
);
[ "$2" ] && { [ "$3" ] || printf "==> ${XJ_GREEN}%s${XJ_RE} <==\n" "$2"; tail -F -n "$@" | __xj_colorless; exit 0; }
fi
set -- -F -n ${1:-$XJ_LOG_PRINT_LAST_LINES}
else
if [ "$xj_java_pid" ]; then
set -- ${1:-$XJ_LOG_PRINT_LAST_LINES} ${2:-0};
set -- $1 $(
eval find $(
__xj_fd_file $xj_java_pid | awk '/\.(LOG|log)$/{
sub(/\/[^\/]+$/, "");
if(!unique[$0]++) line[++n]=$0;
}
END{
for (i=1; i<=asorti(unique, sorted_idx); i++) {
j=0;
while (sorted_idx[i] "/" == substr(sorted_idx[i + ++j], 1, length(sorted_idx[i]) + 1)) {
delete unique[sorted_idx[i + j]];
};
i+=j-1;
};
for(i=length(line); i>0; i--) if(unique[line[i]]) printf " \"%s\"", line[i];
}'
) -type f -mtime ${2#-} -iname "*.log"
);
[ "$2" ] && { [ "$3" ] || printf "==> ${XJ_GREEN}%s${XJ_RE} <==\n" "$2"; tail -n "$@" | __xj_colorless; exit 0; }
fi
set -- -n ${1:-$XJ_LOG_PRINT_LAST_LINES}
fi
xj_jar_log_file="$(date -d "$xj_days days" +$XJ_JAR_LOG_DIR/${xj_jar_name}_%Y%m%d.log)";
printf "==> ${XJ_GREEN}%s${XJ_RE} <==\n" $xj_jar_log_file;
tail $@ "$xj_jar_log_file" 2>/dev/null | __xj_colorless
;;
--rotate|--redirect)
__xj_permission "$@";
__xj_test_command readlink || exit 1;
__xj_test_command dd || exit 1;
__xj_test_command stat || exit 1;
__xj_test_command truncate || exit 1;
__xj_test_command tail || exit 1;
[ "$xj_java_pid" ] || {
[ "$TERM" == "dumb" ] || printf "name:${XJ_BOLD}%-26s${XJ_RE} [${XJ_B_RED}ERROR${XJ_RE}] not running.\n" $xj_jar_name, >&2;
exit 1
};
__xj_test_single_process;
xj_log_writer_pid=$(__xj_log_writer_pid $xj_java_pid);
xj_live_log_file="$(readlink --canonicalize /proc/$xj_log_writer_pid/fd/1 2>/dev/null)";
[ -f "$xj_live_log_file" ] || {
printf "name:${XJ_BOLD}%-26s${XJ_RE} [${XJ_B_RED}ERROR${XJ_RE}] cannot locate the live log file of pid ${XJ_BOLD}%s${XJ_RE}.\n" $xj_jar_name, "$xj_log_writer_pid" >&2;
exit 1
};
__xj_log_is_append $xj_log_writer_pid || {
printf "name:${XJ_BOLD}%-26s${XJ_RE} [${XJ_B_RED}ERROR${XJ_RE}] the live log file is not opened in append mode; rotating it would be unsafe.\n" $xj_jar_name, >&2;
exit 1
};
xj_log_dir="${xj_live_log_file%/*}";
xj_live_log_name="${xj_live_log_file##*/}";
# Strip the trailing _YYYYMMDD.log to keep the log name stable.
xj_log_stem="${xj_live_log_name%_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].log}";
[ "$xj_log_stem" != "$xj_live_log_name" ] || xj_log_stem="$xj_jar_name";
xj_jar_new_log_file="$xj_log_dir/${xj_log_stem}_$(date +%Y%m%d).log";
# The archive keeps the live file's own (previous day) name.
xj_jar_old_log_file="$xj_live_log_file";
# The live log is already named for today: nothing to rotate.
[ "$xj_live_log_file" != "$xj_jar_new_log_file" ] || exit 0;
[ -e "$xj_jar_new_log_file" ] && {
[ -s "$xj_jar_new_log_file" ] && {
printf "name:${XJ_BOLD}%-26s${XJ_RE} [${XJ_B_RED}ERROR${XJ_RE}] target log file '${XJ_GREEN}%s${XJ_RE}' exists and is not empty.\n" $xj_jar_name, "$xj_jar_new_log_file" >&2;
exit 1
};
rm -f "$xj_jar_new_log_file"
};
# Rename the file the process still writes to; its fd follows
# the inode, so the writer is not interrupted.
mv -f "$xj_live_log_file" "$xj_jar_new_log_file" || exit 1;
# --strict (or LOG_ROTATE_STRICT=1) freezes the writer for
# the short critical section of every step, so nothing is
# ever lost. Otherwise the writer keeps running untouched
# and only a tiny night-time window may drop bytes.
xj_log_strict=;
if [ "$3" == "--strict" ]; then
xj_log_strict=$xj_log_writer_pid;
else
case "${XJ_LOG_ROTATE_STRICT}" in 1|[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]) xj_log_strict=$xj_log_writer_pid;; esac;
fi
__xj_log_rotate "$xj_jar_new_log_file" "$xj_jar_old_log_file" "$xj_jar_old_log_file.spool" "$xj_user:$xj_group" "$xj_log_strict" || exit 1;
chmod a-w "$xj_jar_old_log_file" 2>/dev/null;
;;
--cron)
__xj_test_command crontab || exit 1;
__xj_test_command readlink || exit 1;
shift; shift;
[ "$1" ] || {
printf "[${XJ_B_RED}ERROR${XJ_RE}] directory path not set.\n" >&2;
exit 1
}
[ -d "$1" ] || {
printf "[${XJ_B_RED}ERROR${XJ_RE}] '${XJ_GREEN}%s${XJ_RE}' server directory must exist.\n" $1 >&2;
exit 1
};
# TODO: /etc/cron.d/.
# Random start minute plus a per-run random delay, so the
# rotation does not fire on a fixed period and does not
# collide with other cron jobs on the same host. The delay
# is seeded from /dev/urandom, because cron jobs starting
# in the same second would otherwise draw the same value.
xj_rotate_delay=${XJ_LOG_ROTATE_DELAY:-1800};
case "$xj_rotate_delay" in ''|*[!0-9]*|0) xj_rotate_delay=1800;; esac;
if command -v od >/dev/null 2>&1; then
xj_cron_minute=$(awk -v s="$(od -An -N2 -tu2 /dev/urandom)" 'BEGIN{srand(s); print int(rand()*60)}');
xj_cron_sleep="\$(awk -v s=\"\$(od -An -N2 -tu2 /dev/urandom)\" 'BEGIN{srand(s); print int(rand()*$xj_rotate_delay)}')";
else
xj_cron_minute=$(awk 'BEGIN{srand(); print int(rand()*60)}');
xj_cron_sleep="\$(awk 'BEGIN{srand(); print int(rand()*$xj_rotate_delay)}')";
fi
xj_cron_strict=;
case "${XJ_LOG_ROTATE_STRICT}" in 1|[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]) xj_cron_strict=" --strict";; esac;
xj_cron_expression="$xj_cron_minute 0 * * * sleep $xj_cron_sleep; sudo $0 -d ${1%/} log --rotate$xj_cron_strict";
xj_cron_anchor="sudo $0 -d ${1%/} log --";
xj_cron_text=$(crontab -l 2>/dev/null);
if [ "$xj_cron_text" ]; then
# Replace an existing line for the same directory, so
# re-running this upgrades it instead of duplicating.
xj_cron_text=$(awk -v anchor="$xj_cron_anchor" -v new="$xj_cron_expression" '
/^[[:space:]]*#/ { print; next }
{ if (index($0, anchor)) { if (!done++) print new; next } print }
END { if (!done) print new }
' <<< "$xj_cron_text");
printf '%s\n' "$xj_cron_text" | crontab -
else
printf '# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
\n%s\n
' "$xj_cron_expression" | crontab -
fi
crontab -l
;;
--rm)
__xj_permission "$@";
[ "$3" ] || exit 1;
[ $3 -lt 1 ] && exit 1;
xj_last_mod_days="$3";
set --;
[ "$xj_java_pid" ] && \
eval set -- $(__xj_fd_file $xj_java_pid | awk '/(LOG|log)$/{sub(/\/[^\/]+$/, ""); if(!unique[$0]++) printf " \"%s\"", $0}')
[ "$1" ] || set -- "$XJ_JAR_LOG_DIR";
find "$@" -type f -mtime +$xj_last_mod_days -iname "*.log" -printf 'rm -fv "%p" # %TY%Tm%Td\t%s\n' 2>/dev/null | \
awk '{sub(/[0-9]+/, "\t" sprintf("%6.1f", $NF/1024/1024) "M", $NF); print}'
;;
*)
printf "[${XJ_B_RED}ERROR${XJ_RE}] Invalid option: log '${XJ_BROWN}%s${XJ_RE}'\n" "$2" >&2
exit 1
;;
esac
;;
stop)
__xj_permission "$@";
__xj_test_workspace;
[ "$xj_java_pid" ] || { printf "name:${XJ_BOLD}%-26s${XJ_RE} not running.\n" $xj_jar_name; return 1; } >&2;
if [ "$2" == "-f" -o "${XJ_JAR_ARGS/--spring\.config\.location=/}" == "$XJ_JAR_ARGS" ]; then
xj_sigspec=9;
else
xj_sigspec=15;
fi
kill -$xj_sigspec $xj_java_pid $(__xj_pipe_pid $xj_java_pid);
sleep 0.5;
xj_wait_limit=${XJ_STOP_TIMEOUT:-30};
while ps -p $xj_java_pid >/dev/null; do
sleep 1;
[ $((xj_wait_limit--)) -le 0 ] && {
printf "[${XJ_B_RED}ERROR${XJ_RE}] failed to kill '${XJ_BOLD}%s${XJ_RE}', pid: ${XJ_BOLD}%s${XJ_RE}\n" $xj_jar_name $xj_java_pid >&2;
exit 1
};
done
unset xj_wait_limit xj_sigspec;
;;
restart)
__xj_permission "$@";
__xj_test_command java || exit 1;
__xj_test_workspace;
shift;
__xj_main stop;
__xj_main start "$@"
;;
status|"")
__xj_test_workspace;
__xj_test_command readlink || exit 1;
__xj_test_single_process;
if [ "$xj_java_pid" ]; then
{
ps -o ruser:16,pid,nlwp,pcpu,pmem,rss -p $xj_java_pid;
readlink `for xj_fd in /proc/$xj_java_pid/fd/*; do [ -S $xj_fd ] && printf " %s" $xj_fd; done` 2>/dev/null
} | awk -v pre=${PWD##*/} -v arg2="$2" -v XJ_BOLD="$XJ_BOLD" -v XJ_RE="$XJ_RE" '
NR == 2 {
printf "name:"XJ_BOLD"%-26s"XJ_RE" u:"XJ_BOLD"%-12s"XJ_RE" pid:"XJ_BOLD"%-7s"XJ_RE" thrd:"XJ_BOLD"%-6s"XJ_RE" cpu:"XJ_BOLD"%-8s"XJ_RE" mem:"XJ_BOLD"%-8s"XJ_RE XJ_BOLD"%+6s"XJ_RE,
pre ",", $1 ",", $2 ",", $3 ",", $4 "%,", sprintf("%.2f", $6/1024/1024) "g", "(" $5 "%)";
pid=$2;
split("tcp tcp udp udp", proto_arr, /[[:space:]]+/);
}
NR > 2 {
if (/socket:/){
sock_inode[gensub(/[^0-9]+/, "", "g")]++;
} else if ($1$2 == "sllocal_address") {
file_idx++;
} else if (sock_inode[$10]) {
if($4 == "0A") {
port_listen[proto_arr[file_idx]][gensub(/.*:/, "", 1, $2)]=0;
} else if ($4 != "07") {
port_send[proto_arr[file_idx]]["size"]++;
port_send[proto_arr[file_idx]][$3][$4]++;
}
} else if ($4 ~ /^0[^7A]$/) {
port_hex=gensub(/.*:/, "", 1, $3);
if ($4 == "01") untted_port_estd[proto_arr[file_idx]][port_hex]++;
untted_port_alive[proto_arr[file_idx]][port_hex]++;
}
}
END{
for (proto in port_listen) {
for (port_hex in port_listen[proto]) {
if (!tag4listen++) printf ", lsn:";
if (comma4listen++) printf ",";
printf XJ_BOLD"%-6s"XJ_RE, strtonum("0x" port_hex) "/" proto "(" untted_port_estd[proto][port_hex] + 0 "/" untted_port_alive[proto][port_hex] + 0 ")";
}
if (port_send[proto]["size"]) {
if (!tag4send++) printf ", send:";
if (comma4send++) printf ",";
printf XJ_BOLD"%s"XJ_RE, proto "(" port_send[proto]["size"] "/" length(port_send[proto]) - 1 ")"
}
}
printf "\n";
if (arg2 == "-v") {
split("established syn_sent syn_recv fin_wait1 fin_wait2 time_wait close close_wait last_ack listen closing new_syn_recv max_states", tcp_state, /[[:space:]]+/);
for (proto in port_send) {
for (addr in port_send[proto]) {
len=length(addr);
if (len == 13 || substr(addr, 1, 24) == "0000000000000000FFFF0000") {
ipv4="";
period="";
tmp_addr=len == 37 ? substr(addr, 25, 13) : addr;
for (i=7; i>0; i-=2) {
if (period++) ipv4=(ipv4 ".");
ipv4=(ipv4 strtonum("0x" substr(tmp_addr, i, 2)));
}
for (state in port_send[proto][addr]) {
printf "name:"XJ_BOLD"%-26s"XJ_RE" proto:"XJ_BOLD"%-5s"XJ_RE" host:"XJ_BOLD"%-32s"XJ_RE" port:"XJ_BOLD"%-6s"XJ_RE" stat:"XJ_BOLD"%-12s"XJ_RE" count:"XJ_BOLD"%s"XJ_RE"\n",
pre ",", proto ",", ipv4 ",", strtonum("0x" substr(tmp_addr, 10, 4)) ",", tcp_state[strtonum("0x" state)] ",", port_send[proto][addr][state];
}
} else if(len == 37) {
ipv6="";
for(i=1; i<=32; i+=8) {
sub_ip=substr(addr, i, 8);
for (j=7; j>0; j-=2) {
ipv6=(ipv6 substr(sub_ip, j, 2));
if(j==5 || j==1) ipv6=(ipv6 ":");
}
};
sub(/:$/, "", ipv6);
sub(/(:0000)+:/, "::", ipv6);
if (!sub(/^0000::/, "::", ipv6)) sub(/^0+/, "0", ipv6);
gsub(/:0+/, ":0", ipv6);
for (state in port_send[proto][addr]) {
printf "name:"XJ_BOLD"%-26s"XJ_RE" proto:"XJ_BOLD"%-5s"XJ_RE" host:"XJ_BOLD"%-40s"XJ_RE" port:"XJ_BOLD"%-6s"XJ_RE" stat:"XJ_BOLD"%-12s"XJ_RE" count:"XJ_BOLD"%s"XJ_RE"\n",
pre ",", proto ",", tolower(gensub(/:0+([1-9A-Fa-f])/, ":\\1", "g", ipv6)) ",", strtonum("0x" substr(addr, 34, 4)) ",", tcp_state[strtonum("0x" state)] ",", port_send[proto][addr][state];
}
}
}
}
}
}' - /proc/net/{tcp,tcp6,udp,udp6};
[ "$2" == "-v" ] && {
ps -T -o tid,maj_flt,min_flt,etime,psr,pcpu,wchan:42,comm:15 -p $xj_java_pid | \
awk -v pid=$xj_java_pid -v pre=${PWD##*/}, -v XJ_BOLD="$XJ_BOLD" -v XJ_RE="$XJ_RE" 'NR > 1 {
sched_file = "/proc/" pid "/task/" $1 "/sched";
while (getline line < sched_file) {
split(line, arg, /[[:space:]]+/);
if (arg[1] == "se.sum_exec_runtime") {}
}
close(sched_file);
io_file = "/proc/" pid "/task/" $1 "/io";
while (getline line < io_file) {
split(line, arg, /[[:space:]]+/);
}
close(io_file);
printf "name:"XJ_BOLD"%-26s"XJ_RE" tid:"XJ_BOLD"%-7s"XJ_RE" flt:"XJ_BOLD"%-10s"XJ_RE" time:"XJ_BOLD"%-10s"XJ_RE" core:"XJ_BOLD"%03d"XJ_RE", cpu:"XJ_BOLD"%-8s"XJ_RE" kfun:"XJ_BOLD"%-20s"XJ_RE" exec:",
pre, $1 ",", $2 "+" $3 ",", gensub(/:[0-9]+$/, "", 1, $4) ",", $5, $6 "%,", gensub("-", "(Running)", 1, $7) ","; for(i=8; i<=NF; i++) printf XJ_BOLD"%s ", $i; printf XJ_RE"\n"
}';
__xj_fd_file $xj_java_pid | awk -v pre=${PWD##*/}, -v XJ_BOLD="$XJ_BOLD" -v XJ_RE="$XJ_RE" '/^\// && ! /\.jar$/{
if(!unique[$0]++) printf "name:"XJ_BOLD"%-26s"XJ_RE" file:"XJ_BOLD"%s"XJ_RE"\n", pre, $0
}'
}
else
printf "name:${XJ_BOLD}%-26s${XJ_RE} inactive (${XJ_RED}dead${XJ_RE}).\n" ${PWD##*/}, >&2;
exit 1
fi
;;
info)
[ "$2" == "-h" -o "$2" == "--help" ] && { __xj_conf_help; exit 0; }
__xj_test_workspace;
(
XJ_WORKING_DIRECTORY="$PWD";
printf '*.CRON_EXPRESSION: %s\n' "$(crontab -l | awk -F \# '/'"${0//\//\\/}"'[[:space:]]/{print $1}')";
printf '%s.START_COMMAND: nohup < /dev/null java %s >> %s 2>&1 &\n' "${PWD##*/}" "$(__xj_java_args)" "$(date +$XJ_JAR_LOG_DIR/${xj_jar_name}_%Y%m%d.log)";
eval `awk -F '[^0-9A-Z_]' 'BEGIN{printf "export"};{for(i=1; i<=NF; i++){if ($i ~ /^XJ_[0-9A-Z_]+/ && ! unique[$i]++) printf " " $i}}' "$0"`;
awk -v pre=${PWD##*/} -F '[^0-9A-Z_]' '{for(i=1; i<=NF; i++){if ($i ~ /^XJ_[0-9A-Z_]+/ && ! unique[$i]++) {sub(/^XJ_/, "", $i); system("printf \"%s\\n\" \"" pre "." $i ": $XJ_" $i "\"")}}}' "$0"
) | sort
;;
--dir|-d|-r)
shift;
xj_tmp_args=" $* ";
[ "$xj_tmp_args$xj_tmp_args$xj_tmp_args" == "${xj_tmp_args/ --dir /}${xj_tmp_args/ -d /}${xj_tmp_args/ -r /}" ] || {
printf "[${XJ_B_RED}ERROR${XJ_RE}] Recursive option is not allowed." >&2;
__xj_usage_help;
exit 1
};
unset xj_dirs xj_tmp_args;
[ -d "$1" ] || set -- "$PWD" "$@";
# Get all the case-esac arguments.
xj_case_args=$(awk -F \) '/^[[:space:]]*[A-Za-z|-]+)/{gsub(/\|/, " ", $1); printf " %s ", $1}' "$0");
# Trim "$@"; make sure the path exists and does not match a case-esac argument.
while [ -d "$1" -a "${xj_case_args/ $1 /}" == "$xj_case_args" ]; do xj_dirs[${#xj_dirs[@]}]="$1"; shift; done
unset xj_case_args xj_act;
export TEAMCITY_VERSION=none TERM=dumb;
while read xj_jar_dir; do
(
cd "`dirname "$xj_jar_dir"`";
unset xj_dirs xj_jar_dir;
__xj_main "$@";
)
xj_act=true
done < <(
__xj_recurse() {
for xj_path in "$1"/*.jar "$1"/*/; do
[ -f "$xj_path" ] && `__xj_test_zip_head "$xj_path" 2>/dev/null` && {
printf '%s\n' "$xj_path";
return 0
};
[ -d "$xj_path" -a ${2:-0} -lt $XJ_FIND_JAR_MAX_DEPTH ] && __xj_recurse "${xj_path%/}" $(($2 + 1))
done
}
for xj_tmp_dir in "${xj_dirs[@]}"; do __xj_recurse "$xj_tmp_dir"; done
);
[ "$xj_act" ] || \
printf "[${XJ_B_RED}ERROR${XJ_RE}] '${XJ_GREEN}%s${XJ_RE}' no jar file found; the find depth is $XJ_FIND_JAR_MAX_DEPTH.\n" $1 >&2;
unset xj_dirs xj_jar_dir
;;
-h|--help)
__xj_usage_help;
;;
*)
printf "[${XJ_B_RED}ERROR${XJ_RE}] Invalid option '${XJ_BROWN}$1${XJ_RE}'.\n" >&2;
__xj_usage_help;
exit 1
;;
esac
}
__xj_main "$@"