进程启动后使用taskset进行cpu绑定

[root@177qing ~]# cat cpu_affinity.sh 
#!/bin/sh
openresty=/usr/local/openresty/nginx/sbin/nginx
nginx=/usr/local/nginx/sbin/nginx

taskset_affinity_cpu()
{
  exec=$1
  pid=`ps -ef| grep $exec | awk '{if($3=="1") {print $2}}'`
  workpids=`ps -ef| grep $pid | grep worker| awk '{print $2}'`

  cpu_sum=`cat /proc/cpuinfo |grep "processor"|wc -l`
  #cpu_sum=4

  if [[ $cpu_sum -le 1 ]]; then 
    echo -e " cpu count too less. just-> $cpu_sum"
    exit 0
  fi

  i=0
  for wpid in $workpids; do
    c=$(( $i % $cpu_sum)) 
    taskset -cp $c $wpid
    i=$(($i + 1))
  done
}
taskset_affinity_cpu $openresty
taskset_affinity_cpu $nginx

你可能感兴趣的:(shell,linux)