目标
以可控制的多进程执行,达到最大执行时长后停止脚本。
控制参数
PROC: 进程数
MAX_EXEC_TIME: 最大执行时长
#!/bin/bash
## create fd
[[ -e ./fd1 ]] || mkfifo ./fd1
exec 6<> ./fd1
rm -rf ./fd1
PROC=4
for i in `seq 1 ${PROC}`;
do
echo >&6
done
## Define max execuate time here
MAX_EXEC_TIME=50
## echo $$ > PPID
ppid=$(echo $$)
## time tic-toc
{
sleep ${MAX_EXEC_TIME} && exec 6<&- && exec 6>&- && echo "ABORT" && kill $ppid
}&
## echo $! > PID
pid=`echo $!`
################
## task list
for line in `seq 1 10`;
do
read -u6
{
## do something here
echo "start_task_${line}: $!"
sleep 3
echo >&6
}&
done
sleep 1
kill $pid
wait
## close fd read & write
exec 6<&-
exec 6>&-
echo "NORMAL EXIT" && exit 0