SourceKitService running at 100+% CPU?

If you are developing using Xcode and Swift, then you are definitely plagued with the problem of SourceKitService thrashing your CPU to make your MacBooks feel like a 747 getting ready to take off. The entire computer freezes and you start up Activity Monitor to see that the CPU bars are at max and this particular service is at 100+%. I found a hacky way to fix this issue, I am running a script that keeps repeating in the background and checks the CPU usage of SourceKitService and if it exceeds the threshold, it kills the service.

#!/bin/bash

MAXCPU=80
while [ 1 ];
do 
res=$(ps -eo pid -eo pcpu -eo command | grep "SourceKitService" | grep -v grep | cut -b1-5 -b 7-11)
PID=$(echo $res | cut -b1-5)
CPU=$(echo $res | cut -b6-10)

#echo $CPU
if [ ! -z $CPU ] ; then
 #if [ $CPU -ge $MAXCPU ] ; then 
 RES=$(echo "$CPU > $MAXCPU" | bc)
 if [ $RES -eq 1 ] ; then
  echo "Killing SKS for $PID $CPU" | terminal-notifier -timeout 10
  kill -9 $PID
 fi
fi

sleep 1;
done


WHat it does is it lists out the PID and the CPU usage and greps for the name SourceKitService, from that line, it cuts the values of PID and CPU to check for the threshold and if it is kill that PID

Enjoy!! :)

Comments

Popular Posts