I use lscpu | grep 'MHz' to get my clock speed. I am getting different results based on the method I am calling this command.

Method 1: typing it in the terminal
CPU MHz: 1200.000

Method 2: Encapsulating it in a script called test.sh
Script code:

#! /bin/bash
cpu=$(lscpu | grep 'MHz')
echo $cpu

Then, in the terminaL

$ chmod +x test.sh
$ ./test.sh

CPU MHz: 1200.000

So far so good. But now, method 3: hiting the 'run or view current file' in Geany on test.sh. Output:
CPU MHz: 2200.000

Method 4: writing a python script called test.py that calls test.sh.
Python script code:

#! /usr/bin/python
import subprocess

def test():

if __name__ == '__main__':

Then, in the terminal:
$ python test.py

CPU MHz: 2200.000

I've tried both python and python3. Both give the same (erroneous) result.

Why does lscpu give a different output when it is run indirectly? And how can I fix this/circumvent this? Is something wrong in my code? For what it's worth, I am running Ubuntu 13.10.

Thanks :-).

  • Gx1sptDTDa, good question, thanks. But you should really get a simple name, lol I wonder I spell it correctly here in my comment. – beginer Jul 27 '14 at 9:50

That's a coincidence, it's your system changing the current cpu frequency.

Check with this command in a terminal:

watch -n 0 "lscpu | grep 'MHz'"

You will see (when waiting a bit) that the system's cpu frequency is switching.

You can also get your current cpu frequency with this command:

cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq

And your maximum/minimum cpu freq with those:

cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq

Change the behavior? The cpu governor decides what frequency should be used. See this documentation of the linux kernel to decide with cpu governor fits best for you.

  • Get the governor: cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
  • Change the governor: echo "powersave" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
  • Aha! I guess calling an extra interpreter (in the case of python) requires more work, thus the governor ups the frequency? Makes some sense. – Gx1sptDTDa Apr 8 '14 at 14:03

Most modern CPUs can adjust their clock frequency. The default governor on linux is "on demand" which keeps the clock speed low to save power when idle, and speeds it up to do work when needed.

See https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.