Repository navigation
Expand file tree
/
Copy pathrim-psmon
More file actions
executable file
·92 lines (79 loc) · 2.64 KB
/
Copy pathrim-psmon
File metadata and controls
executable file
·92 lines (79 loc) · 2.64 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
#!/usr/bin/env bash
export RED='\033[1;91m'
export BLUE='\033[1;94m'
export GREEN='\033[1;92m'
export YELLOW='\033[1;33m'
export RESETCOLOR='\033[1;00m'
[[ ! "$RUNTTY" =~ tty|pts ]] && \
NOT_TERM=1||NOT_TERM=0
red_msg() { echo -e "${RED}${@}${RESETCOLOR}" ; }
blue_msg() { echo -e "${BLUE}${@}${RESETCOLOR}" ; }
green_msg() { echo -e "${GREEN}${@}${RESETCOLOR}" ; }
yellow_msg() { echo -e "${YELLOW}${@}${RESETCOLOR}" ; }
rpids_ps() {
set -o pipefail
for rpid in "$@"
do
if [[ -n "$rpid" && -d "/proc/$rpid" ]]
then
red_msg "\n==========================================================="
green_msg "Monitoring of processes from RUNPID: ${BLUE}$rpid"
red_msg "-----------------------------------------------------------"
echo "$(ps -fp $("$RUNSTATIC/cpids" "$rpid" 2>/dev/null) 2>/dev/null)"
red_msg "==========================================================="
else norpids="$(( norpids + 1 ))"
fi
done
if [ "$norpids" == "${#@}" ]
then kill -2 $(pgrep -fa watch|grep "rpids_check.*$RPIDSMONPID"|awk '{print$1}') 2>/dev/null
fi
}
rpids_check() {
IFS=$'\n'
if [ -n "$A_RPIDS" ]
then
[[ "$A_RPIDS" =~ ^declare ]] && \
eval "$A_RPIDS" && unset A_RPIDS
rpids_ps "${RPIDS[@]}"
else
RPIDS=($(ls -d "$RUNTMPDIR"/* 2>/dev/null|grep -v "$RUNPID"|awk -F/ '{print$NF}'))
if [ -n "$RPIDS" ]
then rpids_ps "${RPIDS[@]}"
else
green_msg "Waiting for RunImage containers..."
return 1
fi
fi
}
rpids_check_loop() { watch -ctn 0.5 -x bash -c rpids_check $RPIDSMONPID ; }
error_msg() {
echo -e "${RED}[ ERROR ][$(date +"%Y.%m.%d %T")]: $@ $RESETCOLOR"
if [ "$NOT_TERM" == 1 ]
then notify-send -a 'RunImage Error' "$(echo -e "$@"|nocolor)" 2>/dev/null &
fi
}
print_help() {
echo -e "[ Usage ]: rim-psmon [OPTIONS] RUNPIDs
[ Options ]:
-p, --ps Print the list of RunImage processes
-h, --help Show this message"
exit 1
}
unset RPIDS_PS A_RPIDS RPIDS
case "$1" in
-h|--help) print_help ;;
-p|--ps ) shift ; RPIDS_PS=1 ;;
-*) error_msg "Unknown parameter: ${BLUE}$1\n"; print_help ;;
esac
if [ -n "$1" ]
then
RPIDS=("$@")
export A_RPIDS="$(declare -p RPIDS 2>/dev/null)"
fi
export RPIDSMONPID=$BASHPID
export -f rpids_check rpids_ps blue_msg \
red_msg green_msg yellow_msg
if [ "$RPIDS_PS" == 1 ]
then rpids_check $RPIDSMONPID
else rpids_check_loop
fi