-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.sh
More file actions
executable file
·1328 lines (1107 loc) · 46 KB
/
control.sh
File metadata and controls
executable file
·1328 lines (1107 loc) · 46 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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
: '
**************************************
*** Copyright (C) © 2016 Mike R. ***
*** & 2017 ***
*** & 2018 ***
*** & 2019 ***
*** All rights reserved. ***
*** GitHub.com/MikeR-GH ***
**************************************
*** !! Usage allowed !! ***
*** ~ private && commercial ~ ***
*** !! Selling forbidden !! ***
*** ~ for all purposes ~ ***
**************************************
Script tested on Debian 8 (Jessie), Ubuntu 16.10 (Yakkety Yak) and Ubuntu 18.04 (Bionic Beaver)
Also tested usage in Docker Container with Ubuntu 17.10 (Artful Aardvark) / Ubuntu 18.04 (Bionic Beaver) installed.
Example-Filename: control.sh
Example-Execution: ./control.sh
Commandline-Parameters:
Execution: Parameter: Action:
----------------------------------------------------------------------
./control.sh start Starts the server.
./control.sh stop Stops the server.
./control.sh run Run the server in foreground.
./control.sh restart Restarts the server.
./control.sh status Shows: Is the server running?
./control.sh dump-config Dumps the current configuration
./control.sh last-exit-code Returns and displays the last exit code
./control.sh join Joins the application and exits, when waittime was reached or application stops.
./control.sh console Opens the servers console.
./control.sh help Shows the help-page.
./control.sh * (wildcard) Shows you the usage.
'
: ' >>> NOVICE-SETUP <<< '
APPLICATION_NAME="Application-Server"
SCREEN_NAME="ApplicationScreenName" # No dots! No lead with 'K_'!
EXECUTION_FILE="startfile -parameter1 -parameter2"
EXECUTING_USER="root"
SCREEN_KEEPER=false
MIN_ELAPSED_TIME=30
MAXCOUNT_TIME_EXCEEDED=3
RESTART_DELAY=0
RESTART_ONFAILURE_ONLY=true
CONFIG_FILE="control.cfg"
: ' >>> ADVANCED-SETUP <<< '
NOT_RECOMMEND_FORCE_RUN=false
ENABLE_USERDEFINED_STOP=false
function userdefined_stop() { # Params: SCREEN_NAME/SCREEN_NAME_FULL
# For commands to send or so...
# Do something here, that leads to application exit
return 0
}
function custom_printlog() { # Params: LEVEL ID MESSAGE
return 0
}
: '>>> IMPORTANT-NOTE <<<'
: ' PRINTLOG LEVELS:'
: ' - INFO'
: ' - WARN'
: ' MSGID RANGE: 1 - 42'
: ' >>> SCRIPT-SETUP <<< ' # // DONT CHANGE THIS IF YOU DONT KNOW ABOUT THIS SETTINGS
# Regex
REGEX_SCREEN_NAME="^([^[:space:]\.]+)$" # ScreenName
REGEX_SCREEN_NAME_KEEPER="^K_([^[:space:]\.]+)$" # ScreenName-Keeper
REGEX_SCREEN_NAME_FULL="^([0-9]{1,5})\.([^[:space:]\.]+)$" # ScreenName-Full
# tput-COLORS
COLOR_WHITE="$(tput setaf 7 2>/dev/null)"
COLOR_RED="$(tput setaf 1 2>/dev/null)"
COLOR_GREEN="$(tput setaf 2 2>/dev/null)"
COLOR_YELLOW="$(tput setaf 3 2>/dev/null)"
COLOR_CYAN="$(tput setaf 6 2>/dev/null)"
COLOR_BOLD="$(tput bold 2>/dev/null)"
#COLOR_UNDERLINE="$(tput smul 2>/dev/null)"
#COLOR_UNDERLINE_END="$(tput rmul 2>/dev/null)"
#COLOR_ITALIC="$(tput sitm 2>/dev/null)"
#COLOR_ITALIC_END="$(tput ritm 2>/dev/null)"
#COLOR_BLINK="$(tput blink 2>/dev/null)"
#COLOR_REVERSE="$(tput rev 2>/dev/null)"
#COLOR_INVISIBLE="$(tput invis 2>/dev/null)"
COLOR_RESET="$(tput sgr0 2>/dev/null)"
# Preparation
FULL_COMMAND_LINE="${0} ${@}"
: ' >>> INITIAL CODE / DEBUG OPTION <<< '
#if [ ! executed_byKeeperScreen ]; then
: ' >>> SET DEBUG OPTION HERE <<< '
DEBUG=false
CLEAR_DEBUG=true
#fi;
if [ "${DEBUG}" == true ]; then
echo -e "${COLOR_YELLOW}${COLOR_BOLD}[DEBUG]${COLOR_WHITE} Debugging enabled!${COLOR_RESET}"
# noglob
#set -f
# verbose
#set -v
# xtrace
set -x
fi;
if [ "${1}" == "syntax" ]; then
if [ "${2}" == "quiet" ]; then
exit $(bash -n &>/dev/null)
else
exit $(bash -n)
fi;
fi;
BINDIR=$(dirname "$(readlink -fn "$0")")
cd "$BINDIR"
: ' >>> METHODS <<< '
function get_current_user() { # Params: RETURN
local get_current_user_return="";
get_current_user_return=$(whoami)
eval "${1}=\"${get_current_user_return}\""
unset -v get_current_user_return
return 0
}
function user_exists() { # Params: USER
if getent passwd ${1} > /dev/null; then
return 0
fi;
return 1
}
function correct_user_running() {
if [ $(get_current_user RET; echo "${RET}") == "root" ] && [ ${EXECUTING_USER} != "root" ]; then # IF [ user == root and havetouser != root ]
return 1
elif [ $(get_current_user RET; echo "${RET}") == "${EXECUTING_USER}" ]; then # IF [ user == havetouser ]
return 0
else # ELSE: IF [ user != root and user != havetouser ]
return 1
fi;
}
function user_permitted() { # Params: USER
if user_exists "${1}"; then
if [ $(get_current_user RET; echo "${RET}") == "root" ] && [ ${1} != "root" ]; then # IF [ user == root and havetouser != root ]
return 0
elif [ $(get_current_user RET; echo "${RET}") == "${1}" ]; then # IF [ user == havetouser ]
return 0
else # ELSE: IF [ user != root and user != havetouser ]
return 1
fi;
return 1
fi;
}
function redirect_user() { # Params: USER
if user_exists "${1}"; then
if user_permitted "${1}"; then
if ! correct_user_running; then
sudo -u ${1} ${FULL_COMMAND_LINE}
return ${?}
fi;
fi;
fi;
return 1
}
function redirect_processed() {
if [ "${PPID}" -eq "0" ]; then return 1; fi;
PARENT_PID=$(ps -o ppid= ${PPID} | sed -e 's/^[ ]//' )
if [ "${PARENT_PID}" -eq "0" ]; then return 1; fi;
CMDLINE=$(ps -p ${PPID} -o args --no-headers)
PARENT_CMDLINE=$(ps -p ${PARENT_PID} -o args --no-headers)
COMP_CMDLINE="sudo -u ${EXECUTING_USER} ${FULL_COMMAND_LINE}"
COMP_PARENT_CMDLINE="/bin/bash ${FULL_COMMAND_LINE}"
if [ "${CMDLINE}" == "${COMP_CMDLINE}" ] && [ "${PARENT_CMDLINE}" == "${COMP_PARENT_CMDLINE}" ]; then
return 0
fi;
unset -v CMDLINE PARENT_PID PARENT_CMDLINE COMP_CMDLINE COMP_PARENT_CMDLINE
return 1
}
function executed_byBash() {
PARENT_PID=$(ps -o ppid= ${PPID} | sed -e 's/^[ ]//' )
if [ "${PARENT_PID}" -eq "0" ]; then return 1; fi;
CMDLINE=$(ps -p ${PARENT_PID} -o args --no-headers)
COMP_CMDLINE="/bin/bash ./control.sh"
if [ "${#CMDLINE}" -ge 22 ]; then
if [ "${CMDLINE:0:22}" == "${COMP_CMDLINE}" ]; then
return 0
fi;
fi;
unset -v CMDLINE COMP_CMDLINE
return 1
}
function executed_bySSHD() {
if [ "${PPID}" -eq "0" ]; then return 1; fi;
PARENT_PID=$(ps -o ppid= ${PPID} | sed -e 's/^[ ]//' )
if [ "${PARENT_PID}" -eq "0" ]; then return 1; fi;
CMDLINE=$(ps -p ${PARENT_PID} -o args --no-headers)
COMP_CMDLINE="sshd: "
if [ "${#CMDLINE}" -ge 6 ]; then
if [ "${CMDLINE:0:6}" == "${COMP_CMDLINE}" ]; then
return 0
fi;
fi;
unset -v CMDLINE COMP_CMDLINE
return 1
}
function executed_byKeeperScreen() {
CMDLINE=$(ps -p ${PPID} -o args --no-headers)
if [ -z "${1}" ] \
&& ( ( [ "$(echo -e "${CMDLINE}" | awk '{print $1}')" == "SCREEN" ] \
&& [ "$(echo -e "${CMDLINE}" | awk '{print $2}')" == "-admS" ] \
&& [ "$(echo -e "${CMDLINE}" | awk '{print $3}')" == "K_${SCREEN_NAME}" ] \
&& [ "$(basename $(echo -e "${CMDLINE}" | awk '{print substr($0, index($0,$4))}') 2> /dev/null)" == "$(basename ${0} 2> /dev/null)" ] ) \
|| ( [ -e "$(basename $(echo -e "${CMDLINE}" | awk '{print substr($0, index($0,$2))}') 2> /dev/null)" ] \
&& [ "$(basename $(echo -e "${CMDLINE}" | awk '{print substr($0, index($0,$2))}') 2> /dev/null)" == "$(basename ${0} 2> /dev/null)" ] ) ); then
return 0;
fi;
return 1;
}
: '
@Developer Notes
Function: ......... screen_list
Explaination ...... Returns all the screens back, which run on the current user.
Parameters:
RETURN ........ Return-Value (List of all Screens)
Additional Notes:
$(RETURN) | sed -n ${LINE}p ........................................ Get 3rd line of ${RETURN} (In Example: ${LINE} = 3; Rule: ${LINE} > 0)
$(RETURN) | grep -F ".${SCREEN_NAME} " ............................ Get only screens with ${SCREEN_NAME}
grep -c . <<<"$(RETURN)" ............................................ Get lines count of ${RETURN}
echo "${FGET}" | grep -F "${1}" | cut -f1 -d' ' | cut -f1 -d'.' .... Get PID of a screen - Please check for duplicate
'
function screen_list() { # Params: RETURN Optional:SCREEN_NAME
local screen_list_return="";
#FLIST=$( screen -ls | grep -P "^\t([0-9]{1,5})\.([^\t\s\.]+)\t\(([0-9]{2}\/[0-9]{2}\/[0-9]{4})\s([0-9]{2}\:[0-9]{2}\:[0-9]{2})\s(PM)\)\t\(([^\(\)]*)\)$" | sed -e 's/^[ \t]*//' ) # @Deprecated
screen_list_return=$( screen -ls | grep -P "^(\t|\s)+([0-9]{1,})\.([^\t\s\.]+)(\t|\s)+\(([0-9]{2}[\/\.][0-9]{2}[\/\.]([0-9]{2}|[0-9]{4}))\s([0-9]{2}\:[0-9]{2}\:[0-9]{2})(\s(PM|AM))?\)(\t|\s)+\(([^\(\)]*)\)$" | sed -e 's/^[ \t]*//' )
if [[ "${2}" =~ ${REGEX_SCREEN_NAME_FULL} ]]; then
screen_list_return=$( echo "${screen_list_return}" | grep -P "{2}(\t|\s)" )
elif [[ "${2}" =~ ${REGEX_SCREEN_NAME} ]]; then
screen_list_return=$( echo "${screen_list_return}" | grep -P "^([0-9]{1,})\.${2}(\t|\s)" )
fi;
eval "${1}=\"${screen_list_return}\""
unset -v screen_list_return
return 0
}
function screen_count() { # Params: RETURN SCREEN_NAME/SCREEN_NAME_FULL
local screen_count_return="0";
if [[ "${2}" =~ ${REGEX_SCREEN_NAME_FULL} ]]; then
if screen_status ${2}; then
screen_count_return="1"
else
return 1
fi;
elif [[ "${2}" =~ ${REGEX_SCREEN_NAME} ]]; then
screen_list screen_count_return "${2}"
screen_count_return=$( grep -c . <<<"${screen_count_return}" )
else
return 1
fi;
eval "${1}=\"${screen_count_return}\""
unset -v screen_count_return
return 0
}
function screen_pid() { # Params: RETURN SCREEN_NAME/SCREEN_NAME_FULL
local screen_pid_return="0";
if [[ "${2}" =~ ${REGEX_SCREEN_NAME_FULL} ]]; then
if screen_status "${2}"; then
screen_pid_return=$( echo -e "${2}" | cut -f1 -d'.' )
else
return 1
fi;
elif [[ "${2}" =~ ${REGEX_SCREEN_NAME} ]]; then
screen_list screen_pid_return "${2}"
if [ "$( grep -c . <<<"${screen_pid_return}" )" -eq 1 ]; then
screen_pid_return=$( echo -e "${screen_pid_return}" | cut -f1 -d' ' | cut -f1 -d'.' )
else
echo -e "Hier:${2}"
return 1
fi;
else
return 1
fi;
eval "${1}=\"${screen_pid_return}\""
unset -v screen_pid_return
return 0
}
function screen_status() { # Params: SCREEN_NAME/SCREEN_NAME_FULL
local screen_status_list="";
screen_list screen_status_list "${1}"
if [ "$( grep -c . <<<"${screen_status_list}" )" -ne 0 ]; then # IF there does screens exists with this name
return 0
fi;
unset -v screen_status_list
return 1
}
function screen_last_exitcode() { # Params:
if [ -n "${1}" ]; then
local SCREEN_EXIT_FILE="/run/screen/S-${EXECUTING_USER}.screen-exit.${SCREEN_NAME}"
if [ -f "${SCREEN_EXIT_FILE}" ]; then
read -r SCREEN_EXIT_CODE_TMP<"${SCREEN_EXIT_FILE}"
local SCREEN_EXIT_CODE=${SCREEN_EXIT_CODE_TMP}
unset -v SCREEN_EXIT_CODE_TMP
if [ "${SCREEN_EXIT_CODE}" -eq "${SCREEN_EXIT_CODE}" ] &>/dev/null; then
return ${SCREEN_EXIT_CODE}
fi
fi
fi
return 255
}
function screen_start() { # Params: SCREEN_NAME COMMAND_LINE
if [[ ${1} =~ ${REGEX_SCREEN_NAME} ]] && [ "${#}" -ge 2 ]; then
if ! screen_status "${1}"; then
screen -admS ${1} "${@:2}"
return 0
fi;
fi;
return 1
}
function screen_stop() { # Params: SCREEN_NAME/SCREEN_NAME_FULL
if [[ ${1} =~ ${REGEX_SCREEN_NAME} ]] || [[ ${1} =~ ${REGEX_SCREEN_NAME_FULL} ]]; then
if screen_status "${1}"; then
screen -X -S ${1} quit
return 0
fi;
fi;
return 1
}
function screen_reattach() { # Params: SCREEN_NAME/SCREEN_NAME_FULL
if [[ ${1} =~ ${REGEX_SCREEN_NAME} ]] || [[ ${1} =~ ${REGEX_SCREEN_NAME_FULL} ]]; then
if screen_status "${1}"; then
script -q -c "screen -rxS ${1}"
return 0
fi;
fi;
return 1
}
function screen_send_string() { # Params: SCREEN_NAME/SCREEN_NAME_FULL STRING
if ( [[ ${1} =~ ${REGEX_SCREEN_NAME} ]] || [[ ${1} =~ ${REGEX_SCREEN_NAME_FULL} ]] ) && [ ! -z "${2}" ]; then
if screen_status "${1}"; then
screen -S ${1} -X stuff "${2}"
return 0
fi;
fi;
return 1
}
function screen_send_cmd() { # Params: SCREEN_NAME/SCREEN_NAME_FULL COMMAND_LINE
if ( [[ ${1} =~ ${REGEX_SCREEN_NAME} ]] || [[ ${1} =~ ${REGEX_SCREEN_NAME_FULL} ]] ) && [ ! -z "${2}" ]; then
if screen_status "${1}"; then
screen -S ${1} -X stuff "${2}^M"
return 0
fi;
fi;
return 1
}
function get_distrib_name() { # Params: RETURN
local distrib_name_return="";
if [ -f /etc/os-release ]; then
distrib_name_return="$(cat /etc/os-release | grep -P "^NAME=" | cut -f2 -d'=')"
if [ "${distrib_name_return:0:1}" = "\"" ]; then
distrib_name_return="${distrib_name_return:1:${#distrib_name_return}-2}"
fi;
elif [ -f /etc/lsb-release ]; then
distrib_name_return="$(cat /etc/lsb-release | grep -P "^DISTRIB_ID=" | cut -f2 -d'=')"
if [ "${distrib_name_return:0:1}" = "\"" ]; then
distrib_name_return="${distrib_name_return:1:${#distrib_name_return}-2}"
fi;
else
return 1
fi;
eval "${1}=\"${distrib_name_return}\""
unset -v distrib_name_return
return 0
}
function get_distrib_version() { # Params: RETURN
local distrib_version_return="";
if [ -f /etc/os-release ]; then
distrib_version_return="$(cat /etc/os-release | grep -P "^VERSION_ID=" | cut -f2 -d'=')"
if [ "${distrib_version_return:0:1}" == "\"" ]; then
distrib_version_return="${distrib_version_return:1:${#distrib_version_return}-2}"
fi;
elif [ -f /etc/lsb-release ]; then
distrib_version_return="$(cat /etc/lsb-release | grep -P "^DISTRIB_RELEASE=" | cut -f2 -d'=')"
if [ "${distrib_version_return:0:1}" == "\"" ]; then
distrib_version_return="${distrib_version_return:1:${#distrib_version_return}-2}"
fi;
else
return 1
fi;
eval "${1}=\"${distrib_version_return}\""
unset -v distrib_version_return
return 0
}
function get_distrib_version_name() { # Params: RETURN
local distrib_version_name_return="";
f_distrib_name=""
get_distrib_name f_distrib_name
f_distrib_name_lowercase="$(echo "${distrib_name}" | awk '{print tolower($0)}')"
f_distrib_version=""
get_distrib_version f_distrib_version
if [ "${f_distrib_name_lowercase}" == "debian" ] || [ "${f_distrib_name_lowercase}" == "debian gnu/linux" ]; then
if [ "${f_distrib_version}" == "7" ]; then
distrib_version_name_return="Wheezy"
elif [ "${f_distrib_version}" == "8" ]; then
distrib_version_name_return="Jessie"
elif [ "${f_distrib_version}" == "9" ]; then
distrib_version_name_return="Stretch"
elif [ "${f_distrib_version}" == "10" ]; then
distrib_version_name_return="Buster"
elif [ "${f_distrib_version}" == "11" ]; then
distrib_version_name_return="Bullseye"
fi;
elif [ "${f_distrib_name_lowercase}" == "ubuntu" ]; then
if [ "${f_distrib_version}" == "12.04 LTS" ] \
|| [ "${f_distrib_version}" == "12.04.1 LTS" ] \
|| [ "${f_distrib_version}" == "12.04.2 LTS" ] \
|| [ "${f_distrib_version}" == "12.04.3 LTS" ] \
|| [ "${f_distrib_version}" == "12.04.4 LTS" ] \
|| [ "${f_distrib_version}" == "12.04.5 LTS" ]; then
distrib_version_name_return="Precise Pangolin"
elif [ "${f_distrib_version}" == "12.10" ]; then
distrib_version_name_return="Quantal Quetzal"
elif [ "${f_distrib_version}" == "13.04" ]; then
distrib_version_name_return="Raring Ringtail"
elif [ "${f_distrib_version}" == "13.10" ]; then
distrib_version_name_return="Saucy Salamander"
elif [ "${f_distrib_version}" == "14.04 LTS" ] \
|| [ "${f_distrib_version}" == "14.04.1 LTS" ] \
|| [ "${f_distrib_version}" == "14.04.2 LTS" ] \
|| [ "${f_distrib_version}" == "14.04.3 LTS" ] \
|| [ "${f_distrib_version}" == "14.04.4 LTS" ] \
|| [ "${f_distrib_version}" == "14.04.5 LTS" ]; then
distrib_version_name_return="Trusty Tahr"
elif [ "${f_distrib_version}" == "14.10" ]; then
distrib_version_name_return="Utopic Unicorn"
elif [ "${f_distrib_version}" == "15.04" ]; then
distrib_version_name_return="Vivid Vervet"
elif [ "${f_distrib_version}" == "15.10" ]; then
distrib_version_name_return="Wily Werewolf"
elif [ "${f_distrib_version}" == "16.04 LTS" ] \
|| [ "${f_distrib_version}" == "16.04.1 LTS" ] \
|| [ "${f_distrib_version}" == "16.04.2 LTS" ]; then
distrib_version_name_return="Xenial Xerus"
elif [ "${f_distrib_version}" == "16.10" ]; then
distrib_version_name_return="Yakkety Yak"
elif [ "${f_distrib_version}" == "17.04" ]; then
distrib_version_name_return="Zesty Zapus"
elif [ "${f_distrib_version}" == "17.10" ]; then
distrib_version_name_return="Artful Aardvark"
elif [ "${f_distrib_version}" == "18.04" ]; then
distrib_version_name_return="Bionic Beaver"
elif [ "${f_distrib_version}" == "18.10" ]; then
distrib_version_name_return="Cosmic Cuttlefish"
fi;
fi;
if [ -z "${distrib_version_name_return}" ]; then
distrib_version_name_return="Unknown Codename"
fi;
eval "${1}=\"${distrib_version_name_return}\""
unset -v distrib_version_name_return
return 0
}
function require_package() { # Params: PACKAGE_NAME
if ! command -v ${1} &> /dev/null && [ "${NOT_RECOMMEND_FORCE_RUN}" == false ]; then
echo -e "${COLOR_RED}${COLOR_BOLD}The required Package '${1}' isn't installed on your System!${COLOR_RESET}"
script_end 1
fi;
}
function script_end() { # Params: Optional:EXIT_CODE
unset -v APPLICATION_NAME SCREEN_NAME EXECUTION_FILE EXECUTING_USER SCREEN_KEEPER MIN_ELAPSED_TIME RESTART_DELAY
if [ "${DEBUG}" == true ] || [ "${CLEAR_DEBUG}" == true ]; then
# noglob
set +f
# verbose
set +v
# xtrace
set +x
fi;
if [ ! -z "${1}" ] && [ "${1}" -eq "${1}" ] &>/dev/null; then
exit ${1}
fi
exit ${EXIT_CODE}
}
function printlog() { # Params: LEVEL MSGID MESSAGE
if [ "${#}" -eq 3 ] && [ "${2}" -eq "${2}" ] &>/dev/null && declare -F custom_printlog &>/dev/null; then
custom_printlog "${1}" "${2}" "${3}" &>/dev/null
return ${?}
fi
return 255
}
: ' >>> SCRIPT <<< '
EXIT_CODE=0 # DO NOT CHANGE THIS
distrib_name=""
get_distrib_name distrib_name
distrib_name_lowercase="$(echo "${distrib_name}" | awk '{print tolower($0)}')"
distrib_version=""
get_distrib_version distrib_version
distrib_version_name=""
get_distrib_version_name distrib_version_name
# Distribution Check
if [ "${distrib_name_lowercase}" != "ubuntu" ] && [ "${distrib_name_lowercase}" != "debian" ] && [ "${distrib_name_lowercase}" != "debian gnu/linux" ]; then
if [ "${NOT_RECOMMEND_FORCE_RUN}" == false ]; then
echo -e ""
echo -e "${COLOR_RED}${COLOR_BOLD} Your System is not intended to run this Script!${COLOR_RESET}"
echo -e "${COLOR_WHITE}${COLOR_BOLD}----------------------------------------------------------------"
echo -e "${COLOR_WHITE}${COLOR_BOLD} Current Distribution: ${COLOR_YELLOW}${distrib_name} ${distrib_version} ${distrib_version_name}*${COLOR_RESET}"
echo -e "${COLOR_WHITE}${COLOR_BOLD} Supported Distribution: ${COLOR_YELLOW}Debian 7+ or Ubuntu 18.04+${COLOR_RESET}"
echo -e "${COLOR_WHITE}${COLOR_BOLD}----------------------------------------------------------------"
echo -e "${COLOR_RED}${COLOR_BOLD} Setting 'NOT_RECOMMEND_FORCE_RUN' to 'true' forces the Script running!${COLOR_RESET}"
echo -e ""
printlog "WARN" 1 "${APPLICATION_NAME}: Failed: System Unsupported" &>/dev/null
script_end 1
fi;
fi;
# Import CONFIG_FILE, if existing
if [ -f "${CONFIG_FILE}" ]; then
eval "`. ${CONFIG_FILE}&>/dev/null
[ -v APPLICATION_NAME ] && declare -p APPLICATION_NAME 2>/dev/null
[ -v SCREEN_NAME ] && declare -p SCREEN_NAME 2>/dev/null
[ -v EXECUTION_FILE ] && declare -p EXECUTION_FILE 2>/dev/null
[ -v EXECUTING_USER ] && declare -p EXECUTING_USER 2>/dev/null
[ -v SCREEN_KEEPER ] && declare -p SCREEN_KEEPER 2>/dev/null
[ -v MIN_ELAPSED_TIME ] && declare -p MIN_ELAPSED_TIME 2>/dev/null
[ -v MAXCOUNT_TIME_EXCEEDED ] && declare -p MAXCOUNT_TIME_EXCEEDED 2>/dev/null
[ -v RESTART_DELAY ] && declare -p RESTART_DELAY 2>/dev/null
[ -v RESTART_ONFAILURE_ONLY ] && declare -p RESTART_ONFAILURE_ONLY 2>/dev/null
[ -v NOT_RECOMMEND_FORCE_RUN ] && declare -p NOT_RECOMMEND_FORCE_RUN 2>/dev/null
[ -v ENABLE_USERDEFINED_STOP ] && declare -p ENABLE_USERDEFINED_STOP 2>/dev/null
[ "$(type -t custom_printlog)" == "function" ] && declare -f custom_printlog 2>/dev/null
[ "$(type -t userdefined_stop)" == "function" ] && declare -f userdefined_stop 2>/dev/null`"
fi
# Check for Dependencies
require_package "screen"
require_package "sudo"
# Check for Permissions
if ! user_permitted "${EXECUTING_USER}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}You are not permitted to run this Script!${COLOR_RESET}"
printlog "WARN" 2 "${APPLICATION_NAME}: Failed: Insufficient Permission" &>/dev/null
script_end 1
fi;
# Check for valid SCREEN_NAME
if ! [[ ${SCREEN_NAME} =~ ${REGEX_SCREEN_NAME} ]] || [[ ${SCREEN_NAME} =~ ${REGEX_SCREEN_NAME_KEEPER} ]]; then
echo -e "${COLOR_RED}${COLOR_BOLD}Invalid Name for Screen, given in Variable 'SCREEN_NAME'!${COLOR_RESET}\n"
printlog "WARN" 3 "${APPLICATION_NAME}: Failed: Invalid SCREEN_NAME" &>/dev/null
script_end 1
fi;
# Check if parent Process is a Shell
if executed_bySSHD; then
echo -e "${COLOR_CYAN}${COLOR_BOLD}==================== [ ${SCREEN_NAME} | ${EXECUTING_USER} ] ====================${COLOR_RESET}\n"
fi;
# Check if has to change user
if ! redirect_processed; then
if ! correct_user_running; then
if user_exists "${EXECUTING_USER}"; then
echo -e "${COLOR_YELLOW}${COLOR_BOLD}Trying to change the user authority...${COLOR_RESET}"
redirect_user "${EXECUTING_USER}"
script_end ${?}
else
echo -e "${COLOR_RED}${COLOR_BOLD}User doesn't exists, given in Variable 'EXECUTING_USER'!${COLOR_RESET}"
printlog "WARN" 4 "${APPLICATION_NAME}: Failed: User does not exist" &>/dev/null
script_end 1
fi;
fi;
else
if ! correct_user_running; then
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to change the user authority!${COLOR_RESET}"
printlog "WARN" 5 "${APPLICATION_NAME}: Failed: " &>/dev/null
script_end 1
else
echo -e "${COLOR_GREEN}${COLOR_BOLD}Successfully changed the user authority!${COLOR_RESET}\n"
fi;
fi;
# Check for command case
case "${1}" in
start)
if screen_status "${SCREEN_NAME}"; then # IF [ SCREEN is running ] OR [ K_SCREEN is running ]
echo -e "${COLOR_RED}${COLOR_BOLD}The ${APPLICATION_NAME} is already running!${COLOR_RESET}"
printlog "INFO" 6 "${APPLICATION_NAME}: Already running" &>/dev/null
script_end 0
fi;
# Start the Screen
echo -en "${COLOR_WHITE}${COLOR_BOLD}Trying to start the ${APPLICATION_NAME}" # ${COLOR_RESET}"
screen_start "${SCREEN_NAME}" sh -c "${EXECUTION_FILE}; echo \${?}>/run/screen/S-${EXECUTING_USER}.screen-exit.${SCREEN_NAME}"
counter=1
while [ "$counter" -le 10 ]; do
if ! screen_status "${SCREEN_NAME}"; then
echo -en "."
sleep 1
else
break
fi;
counter=$(($counter+1))
done;
unset -v counter
echo -e "${COLOR_RESET}"
if ! screen_status "${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to start the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "WARN" 7 "${APPLICATION_NAME}: Failed to start" &>/dev/null
script_end 1
else
echo -e "${COLOR_GREEN}${COLOR_BOLD}Successfully started the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "INFO" 8 "${APPLICATION_NAME}: Successfully started" &>/dev/null
EXIT_CODE=0
fi;
# Start the Keeper-Screen
if [ "${SCREEN_KEEPER}" == true ]; then
if screen_status "K_${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}The Keeper for the ${APPLICATION_NAME} is already running!${COLOR_RESET}"
printlog "INFO" 9 "${APPLICATION_NAME}: Keeper already running" &>/dev/null
script_end 0
fi;
echo -en "${COLOR_WHITE}${COLOR_BOLD}Trying to start the Keeper for the ${APPLICATION_NAME}"
screen_start "K_${SCREEN_NAME}" "${0}"
counter=1
while [ "$counter" -le 10 ]; do
if ! screen_status "K_${SCREEN_NAME}"; then
echo -en "."
sleep 1
else
break
fi;
counter=$(($counter+1))
done;
unset -v counter
echo -e "${COLOR_RESET}"
if ! screen_status "K_${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to start the Keeper for the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "WARN" 10 "${APPLICATION_NAME}: Failed to start Keeper" &>/dev/null
script_end 1
else
echo -e "${COLOR_GREEN}${COLOR_BOLD}Successfully started the Keeper for the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "INFO" 11 "${APPLICATION_NAME}: Successfully started Keeper" &>/dev/null
script_end 0
fi;
fi;
;;
stop)
if ! screen_status "${SCREEN_NAME}" && ! screen_status "K_${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}The ${APPLICATION_NAME} and the Keeper isn't running!${COLOR_RESET}"
printlog "INFO" 12 "${APPLICATION_NAME}: Unable to stop: Not running" &>/dev/null
script_end 0
fi;
# Stop the Keeper
if ! screen_status "K_${SCREEN_NAME}"; then # IF [ SCREEN isn't running ]
if [ "${SCREEN_KEEPER}" == true ]; then
echo -e "${COLOR_RED}${COLOR_BOLD}The Keeper of the ${APPLICATION_NAME} isn't running!${COLOR_RESET}"
printlog "INFO" 13 "${APPLICATION_NAME}: Unable to stop Keeper: Not running" &>/dev/null
fi;
else
echo -en "${COLOR_WHITE}${COLOR_BOLD}Trying to stop the Keeper of the ${APPLICATION_NAME} by sending a Term-Signal"
SCREEN_PID=""
if ! screen_pid SCREEN_PID "K_${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to stop the Keeper of the ${APPLICATION_NAME} while getting the PID of the screen!${COLOR_RESET}"
printlog "WARN" 14 "${APPLICATION_NAME}: Failed to stop Keeper" &>/dev/null
else
kill -SIGTERM "${SCREEN_PID}" 2> /dev/null
counter=1
while [ "$counter" -le 10 ]; do
if screen_status "K_${SCREEN_NAME}"; then
echo -en "."
sleep 1
else
break
fi;
counter=$(($counter+1))
done;
unset -v counter
echo -e "${COLOR_RESET}"
if ! screen_status "K_${SCREEN_NAME}"; then
echo -e "${COLOR_GREEN}${COLOR_BOLD}Successfully stopped the Keeper of the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "INFO" 15 "${APPLICATION_NAME}: Successfully stopped Keeper: via Term-Signal" &>/dev/null
else
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to stop the Keeper of the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "WARN" 16 "${APPLICATION_NAME}: Failed to stop Keeper: via Term-Signal" &>/dev/null
if [ "${2}" == "force" ] || [ "${2}" == "brute-force" ]; then
echo -en "${COLOR_WHITE}${COLOR_BOLD}Trying to stop the Keeper of the ${APPLICATION_NAME} by sending a Quit-Signal"
kill -SIGQUIT "${SCREEN_PID}" 2> /dev/null
counter=1
while [ "$counter" -le 10 ]; do
if screen_status "K_${SCREEN_NAME}"; then
echo -en "."
sleep 1
else
break
fi;
counter=$(($counter+1))
done;
unset -v counter
echo -e "${COLOR_RESET}"
if ! screen_status "K_${SCREEN_NAME}"; then
echo -e "${COLOR_GREEN}${COLOR_BOLD}Successfully stopped the Keeper of the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "INFO" 17 "${APPLICATION_NAME}: Successfully stopped Keeper: via Quit-Signal" &>/dev/null
else
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to stop the Keeper of the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "WARN" 18 "${APPLICATION_NAME}: Failed to stop Keeper: via Quit-Signal" &>/dev/null
if [ "${2}" == "brute-force" ]; then
echo -en "${COLOR_WHITE}${COLOR_BOLD}Trying to stop the Keeper of the ${APPLICATION_NAME} by sending a Kill-Signal"
kill -SIGKILL "${SCREEN_PID}" 2> /dev/null
counter=1
while [ "$counter" -le 10 ]; do
if screen_status "K_${SCREEN_NAME}"; then
echo -en "."
sleep 1
else
break
fi;
counter=$(($counter+1))
done;
unset -v counter
echo -e "${COLOR_RESET}"
if screen_status "K_${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to stop the Keeper of the ${APPLICATION_NAME}!${COLOR_RESET}"
echo -e "${COLOR_RED}${COLOR_BOLD}Finally the Keeper of the ${APPLICATION_NAME} is still running!${COLOR_RESET}"
printlog "WARN" 19 "${APPLICATION_NAME}: Failed to stop Keeper: via Kill-Signal" &>/dev/null
else
echo -e "${COLOR_GREEN}${COLOR_BOLD}Finally stopped the Keeper of the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "INFO" 20 "${APPLICATION_NAME}: Finally stopped Keeper: via Kill-Signal" &>/dev/null
fi;
fi;
fi;
fi;
fi;
fi;
unset -v SCREEN_PID
fi;
# Stop the Application
if ! screen_status "${SCREEN_NAME}"; then # IF [ SCREEN isn't running ]
echo -e "${COLOR_RED}${COLOR_BOLD}The ${APPLICATION_NAME} isn't running!${COLOR_RESET}"
printlog "INFO" 21 "${APPLICATION_NAME}: Unable to stop: Not running" &>/dev/null
else
if [ "${ENABLE_USERDEFINED_STOP}" == true ]; then
echo -en "${COLOR_WHITE}${COLOR_BOLD}Trying to stop the ${APPLICATION_NAME}"
userdefined_stop "${SCREEN_NAME}"
if [ "${?}" -eq 0 ]; then
counter=1
while [ "$counter" -le 10 ]; do
if screen_status "${SCREEN_NAME}"; then
echo -en "."
sleep 1
else
break
fi;
counter=$(($counter+1))
done;
unset -v counter
echo -e "${COLOR_RESET}"
fi
fi;
if ! screen_status "${SCREEN_NAME}" && [ "${ENABLE_USERDEFINED_STOP}" == true ]; then
echo -e "${COLOR_GREEN}${COLOR_BOLD}Successfully stopped the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "INFO" 22 "${APPLICATION_NAME}: Successfully stopped: via userdefined stop" &>/dev/null
script_end 0
else
if [ "${ENABLE_USERDEFINED_STOP}" == true ]; then
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to stop the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "WARN" 23 "${APPLICATION_NAME}: Failed to stop: via userdefined stop" &>/dev/null
EXIT_CODE=1
else
echo -e "${COLOR_RED}${COLOR_BOLD}No userdefined stop was set!${COLOR_RESET}"
fi;
if [ "${2}" == "force" ] || [ "${2}" == "brute-force" ] || [ "${ENABLE_USERDEFINED_STOP}" == false ]; then
echo -en "${COLOR_WHITE}${COLOR_BOLD}Trying to stop the ${APPLICATION_NAME} by sending a Term-Signal"
SCREEN_PID=""
if ! screen_pid SCREEN_PID "${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to stop the ${APPLICATION_NAME} while getting the PID of the screen!${COLOR_RESET}"
printlog "WARN" 24 "${APPLICATION_NAME}: Failed to stop: Couldn't retrieve the screen PID" &>/dev/null
script_end 1
else
kill -SIGTERM "${SCREEN_PID}" 2> /dev/null
counter=1
while [ "$counter" -le 10 ]; do
if screen_status "${SCREEN_NAME}"; then
echo -en "."
sleep 1
else
break
fi;
counter=$(($counter+1))
done;
unset -v counter
echo -e "${COLOR_RESET}"
if ! screen_status "${SCREEN_NAME}"; then
echo -e "${COLOR_GREEN}${COLOR_BOLD}Successfully stopped the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "INFO" 25 "${APPLICATION_NAME}: Successfully stopped: via Term-Signal" &>/dev/null
script_end 0
else
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to stop the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "WARN" 26 "${APPLICATION_NAME}: Failed to stop: via Term-Signal" &>/dev/null
EXIT_CODE=1
if [ "${2}" == "brute-force" ]; then
echo -en "${COLOR_WHITE}${COLOR_BOLD}Trying to stop the ${APPLICATION_NAME} by sending a Kill-Signal"
kill -SIGKILL "${SCREEN_PID}" 2> /dev/null
counter=1
while [ "$counter" -le 10 ]; do
if screen_status "${SCREEN_NAME}"; then
echo -en "."
sleep 1
else
break
fi;
counter=$(($counter+1))
done;
unset -v counter
echo -e "${COLOR_RESET}"
if screen_status "${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}Failed to stop the ${APPLICATION_NAME}!${COLOR_RESET}"
echo -e "${COLOR_RED}${COLOR_BOLD}Finally the ${APPLICATION_NAME} is still running!${COLOR_RESET}"
printlog "WARN" 27 "${APPLICATION_NAME}: Failed to stop: via Kill-Signal" &>/dev/null
script_end 1
else
echo -e "${COLOR_GREEN}${COLOR_BOLD}Finally stopped the ${APPLICATION_NAME}!${COLOR_RESET}"
printlog "INFO" 28 "${APPLICATION_NAME}: Finally stopped: via Kill-Signal" &>/dev/null
fi;
fi;
fi;
fi;
unset -v SCREEN_PID
fi;
fi;
fi;
;;
run)
if screen_status "${SCREEN_NAME}" || screen_status "K_${SCREEN_NAME}"; then
echo -e "${COLOR_RED}${COLOR_BOLD}The ${APPLICATION_NAME} or the Keeper is running!${COLOR_RESET}"
printlog "WARN" 29 "${APPLICATION_NAME}: Unable to run: ${APPLICATION_NAME} or Keeper is already running" &>/dev/null
script_end 0
fi;
# Variables
COUNT_TIME_EXCEEDED=0
LAST_START_TIME=$(($(date +%s) - ${MIN_ELAPSED_TIME}))
${EXECUTION_FILE}
if [ "${2}" == "keeper" ]; then
WHILE_ENABLED=true
# Traps
trap "WHILE_ENABLED=false" SIGINT SIGKILL SIGTERM SIGQUIT