-
Notifications
You must be signed in to change notification settings - Fork 8
/
lwexec
executable file
·109 lines (101 loc) · 3.55 KB
/
lwexec
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
#!/usr/bin/env bash
PATH="$PATH:$RUNSTATIC"
JOBNUMFL="$LWEXECFLDIR/job"
get_child_pids() {
local child_pids="$(ps --forest -o pid= -g $(ps -o sid= -p $1 2>/dev/null) 2>/dev/null)"
echo -e "$1\n$(ps -o pid=,cmd= -p $child_pids 2>/dev/null|sort -n|\
sed "0,/$1/d" 2>/dev/null|grep -Pv '\d+ sleep \d+'|awk '{print$1}')"|sort -nu
}
try_kill() {
ret=1
if [ -n "$1" ]
then
for pid in $1
do
trykillnum=1
while [[ -n "$pid" && -d "/proc/$pid" ]]
do
if [[ "$trykillnum" -le 3 ]]
then
kill -2 $pid 2>/dev/null
ret=$?
sleep 0.05
else
kill -9 $pid 2>/dev/null
ret=$?
wait $pid &>/dev/null
wait_pid "$pid"
fi
trykillnum="$(( $trykillnum + 1 ))"
done
done
fi
return $ret
}
wait_pid() {
if [ -n "$1" ]
then
if [ "$UNLIM_WAIT" == 1 ]
then
while [ -d "/proc/$1" ]; do sleep 0.1; done
else
[ -n "$2" ] && \
timeout="$2"||
timeout="100"
waittime=1
while [[ -d "/proc/$1" && "$waittime" -le "$timeout" ]]
do
sleep 0.01
waittime="$(( $waittime + 1 ))"
done
fi
fi
}
killjobpids() { try_kill "$(get_child_pids "$execjobpid")" ; }
check_args_spaces() {
for arg in "$@"
do
(echo "$arg"|grep -Po '\s' &>/dev/null||\
echo "$arg"|grep -Eo '.*(.*|.*).*' &>/dev/null) && \
arg="$(echo "$arg"|sed 's|.*|"&"|' 2>/dev/null)"
args+=("$arg")
done
}
cleanup() { [[ -d "$execjobdir" && -n "$jobnum" ]] && rm -rf "$execjobdir" 2>/dev/null ; }
if [[ -d "$LWEXECFLDIR" && -e "$JOBNUMFL" ]]
then
trap 'killjobpids;cleanup' SIGINT SIGTERM
trap 'cleanup' EXIT
unset args
jobnum="$(cat "$JOBNUMFL" 2>/dev/null)"
if [ -n "$jobnum" ]
then
execjobdir="$LWEXECFLDIR/$jobnum"
execenvfl="$execjobdir/env"
execjobfl="$execjobdir/exec"
execjoboutfl="$execjobdir/out"
execjobstatfl="$execjobdir/stat"
export -p|\
grep -v 'SHELL='|\
grep -v 'DISPLAY='|\
grep -v 'XDG_DATA_DIRS='|\
grep -v 'XDG_CONFIG_DIRS='|\
grep -v 'XDG_RUNTIME_DIR='|\
grep -v 'DBUS_SESSION_BUS_ADDRESS=' \
> "$execenvfl" 2>/dev/null
if [ -n "$1" ]
then
check_args_spaces "$@"
echo "${args[@]}" > "$execjobfl"
else cat > "$execjobfl" 2>/dev/null
fi
execjobpid="$(cat "$execjobstatfl" 2>/dev/null)"
cat "$execjoboutfl" 2>/dev/null
execstat="$(cat "$execjobstatfl" 2>/dev/null)"
fi
[ -n "$execstat" ]||execstat=1
exit $execstat
else
echo "LWEXECFLDIR is not configured!"
exit 1
fi