linux shell 实现后台开始,和停止多进程
知识点:
xxxxcmd.sh & //后台运行程序
echo $! // 打印上一次后台运行程序的进程pid
实现:
//start.sh //开始10个进程#!/bin/bash
./do10.sh >> log.log &
echo $! > COSDNA.pid //把后台启动的进程pid,保存起来
//stop.sh //停止运行的10个进程
cat ./COSDNA.pid | xargs -IX kill -9 X
ps -a| grep php | awk '{print $1}'|xargs -IX kill -9 X
//do10.sh //网络开启10个进程采集
declare -i snum=6;
for i in {1..$snum1}
do
php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
sleep $snum;php demo_topage.php `./get_one.sh`&
echo $i;
wait
done
//get_one.sh //实现每次从.txt文件中,独锁读取一行
flock -x /tmp/.start-cosdna.lock -c "sed -n '1p' keylist.txt;sed -i '1d' keylist.txt;";