by doing

for file in /sys/devices/system/cpu/cpu*/online; do echo 0 > $file; done

we set the cpus offline. is there a script to do the hotplug work, detect cpu usage and above a % set cpus online and below a % usage set again cpu offline ? if there isn't any script already made, is there a way to detect current cpu usage only and "do" something if usage is above ...% ?

~EDITING my question:

i am currently here:


while true
 if (echo $[100-$(vmstat 1 2|tail -1|awk '{print $15}')] > 5) then
 for file in /sys/devices/system/cpu/cpu*/online; do echo 1 > $file; done
 if (echo $[100-$(vmstat 1 2|tail -1|awk '{print $15}')] < 5) then
 for file in /sys/devices/system/cpu/cpu*/online; do echo 0 > $file; done
 sleep 3

pfff it should work....

can you please correct me ?

  • nvm i found a way to output current cpu usage using : echo $[100-$(vmstat 1 2|tail -1|awk '{print $15}')] , if there is any hotplug script already made , please inform me. if not, imma make one now for everyone Feb 2, 2017 at 6:18
  • i used a low cpu usage for testing purposes until i get it to work. later i'll set it to 80. can you please tell me why it executes both "if" and the compare ">" "<" doesn't work ? Feb 2, 2017 at 7:11
  • What is your purpose here? With modern processors idle CPUs can go into a deep C state that consumes very little, or even no (if new enough), power. And the reaction time is much much faster than what you are attempting. Feb 2, 2017 at 17:15
  • my cpu is old and cannot do that alone. i thought maybe energy consumtion was less with this script. it could double the battery life of an older quad core laptop that the cpu is always online Feb 3, 2017 at 8:00
  • ok i got it. my wrong was the echo command. without echo its working. so now im answering my question Feb 3, 2017 at 23:27

1 Answer 1


Finally. This is the 1st cpu hotplug for ubuntu. It's not much, but it's the best i can do for now for the older multi core processors that have big battery drain.

Download the script from the link below, make it executable and then copy paste it in /etc/init.d using terminal.

sudo cp /<path to the script>/hotplug.sh /etc/init.d

then update rc.d to execute it on boot

sudo update-rc.d hotplug.sh defaults

done. now your laptop will consume a lot less battery than before.

There is no need to improve the script further and add a check for each cpu core , because it will increase load. Newer cpus have a C state to do that and that doesn't have a big impact to the load but in this case if we make a huge script to run all the time without rest, it will definately increase load and we will lose stability and smoothness.

Download link

  • script is working on all linux distros and the installation way , inside init.d is also universal for all linux distros. Feb 3, 2017 at 23:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .