rc скрипт запуска дочерных команд и их останова из файла со списком pid

 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
#!/bin/bash
dir="$PWD/logs"
cmd="${PWD}/usapat_indexator.sh"
file="usapat.cdlist"
dmn="${PWD}./usapat_master.sh"
partsfile="${dir}/parts"
pidfile="${dir}/usapat.pid"
log="${dir}/usapat.log"
if [[ ! -x ${cmd} ]]; then
echo "Invalid comand"
exit 1
fi
log () {
echo "[$(date)] $@" >> ${log}
}
elog (){
echo "$@"
log "$@"
}
enlog (){
echo -n "$@"
log "$@"
}
run() {
#echo > ${pidfile}
elog "starting USApat indexator"
parts=$(find ${dir} -name ${file}.* -type f -print)
runned=0
all=0
for cdlist in $parts; do
all=$(( ${all} + 1 ))
elog "(${all}) starting ${cdlist}"
${cmd} ${cdlist} &
job=${!}
elog "(${all}) PID: ${job}"
done
[[ ${all} != 0 ]] && jobs -p > ${pidfile} || exit 1
# for job in $(jobs -p); do
# if wait ${job}; then
# runned=$(( ${runned} + 1 ))
# # echo ${job} >> ${pidfile}
# fi
# done
# elog "runned ${runned} / ${all}"
exit 0
}
do_start(){
if run; then
elog "[OK]"
else
elog "[ERROR]"
exit 1
fi
# wait
}
do_stop() {
enlog "stopping USApat"
if [[ -s ${pidfile} ]]; then
while read pid < ${pidfile}; do
log "killling ${pid}"
kill -s TERM ${pid} && sed -ie "\|^${pid}\$|d" ${pidfile} || exit 2
echo -n "."
sleep 1
done && rm ${pidfile}
elog "[DONE]"
else
elog
elog "No pid file"
exit 1
fi
exit 0
}
do_restart(){
do_stop
do_start
}
case $1 in
start) do_start
;;
stop) do_stop
;;
restart) do_restart
;;
*) echo "Unknown command, usage $0 start|stop|restart"
esac