Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

So, I considered asking here instead of SO because people here are more likely to be familiar with this stuff.

I'm currently using PHP to detect some generic stuff on the host OS. In this case, I'm trying to detect CPU vendor.

To do this, I have:

  • linux: execute('grep -m 1 vendor_id /proc/cpuinfo')
  • windows: getenv('PROCESSOR_IDENTIFIER')
  • osx: ???

OSX is indeed my problem. I don't have a test machine, and I refuse to run an illicit VM (I already am using 11 legit VMs).

So, since Apple makes it impossible to do this without spending nothing less than $500 to be able to run osx, I've decided to resort to someone else to help me achieve this.

I'd like the command to work on both PPC and Intel models.

Related Thread on SO again, I'm not able to run any of those commands.

share|improve this question

3 Answers 3

I'm answering my question because I got to the best fit answer, but not without the help of the two people above. So, thanks a lot guys.

Note: There's an additional command, because I found out that on the old PPC version, there's no "Processor Name", but instead, it is "CPU Type". Command system_profiler SPHardwareDataType | grep CPU\ Type fixed the problem. I just switch to it if the suggested one doesn't yield results.

Here's the code:

// ...
case 'osx':
    // check on OSX
    $res=self::execute('system_profiler SPHardwareDataType | grep Processor\ Name');
    $procid=trim(strtolower($res['stdout']));
    if($procid==''){ // for older PPC-based models
        $res=self::execute('system_profiler SPHardwareDataType | grep CPU\ Type');
        $procid=trim(strtolower($res['stdout']));
    }
    break;
// ...

// match vendor
if(strpos($procid,'intel')  !==false)return 'intel';
if(strpos($procid,'amd')    !==false)return 'amd';
if(strpos($procid,'ppc')    !==false)return 'ppc';
if(strpos($procid,'powerpc')!==false)return 'ppc';

Dependency Note

I'm using a custom function for executing commands. It returns an array of result, stdout, stderr among other things. In this case, only stdout is being used, so you can as well replace it with shell_exec().

share|improve this answer

A faster way then the previous post:

system_profiler SPHardwareDataType | grep Processor\ Name

That limits it to only scanning the hardware, which is faster than scanning the whole system.

You can also change the first command to system_profiler -xml SPHardwareDataType if you want to read it in as XML and parse it that way (which should be more stable if the output changes from version-to-version.

share|improve this answer
    
Thanks, SPHardwareDataType did some great improvement in speed. –  Christian Jul 28 '11 at 7:42

Try this from the terminal or execute as needed:

system_profiler|grep Processor

Sample:

> dmourati$ system_profiler|grep Processor
>       Processor Name: Intel Core 2 Duo
>       Processor Speed: 2 GHz
>       Number Of Processors: 1
share|improve this answer
    
Sounds great! Can grep be made to read from after Processor Name: till the next newline? Also, if someone could test this on a PPC and verify that it works, it would be great. –  Christian Jul 28 '11 at 6:57
2  
system_profiler|grep Processor | sed 's/Processor Name: //' –  Iain Jul 28 '11 at 7:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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