Visit Our UNIX and Linux User Community


writing a simple script to get total number of cpus/cores in server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting writing a simple script to get total number of cpus/cores in server
# 1  
Old 01-19-2012
writing a simple script to get total number of cpus/cores in server

Hi,

I am very new to scripting and I wanted to write a unix shell script which can give me,

1)number of cpu's in a box

2)number of cores per cpu

3)total number of cores in abox (ie multiplying 1&2)

I am also trying to figure out how to check if hyper-threading is enabled in the server and also this check to script.

intially I am looking this to run on linux then wanted to implement it on other flavours of unix.

below is my script..
Code:
#!/bin/sh
if [ ! -r /proc/cpuinfo ]; then
  echo "Is this Linux? Cannot find or read /proc/cpuinfo"
  exit 1
fi
echo num_cpus = `grep 'physical id' /proc/cpuinfo | sort -u | wc -l`
read num_cpu
echo num_cores_per_cpu=`grep 'core id' /proc/cpuinfo | sort -u | wc -l`
read num_cores_per_cpu
echo total_cores=`expr $num_cpus \\* num_cores_per_cpu` --this part is not working..

if [ $num_cores -eq 0 ]; then
# this box is either an old SMP or single-CPU box, so count the # of processors

  echo num_cores=`grep '^processor' /proc/cpuinfo | sort -u | wc -l`

fi
#echo $num_cores_per_cpu

any help is much appreciated.

thanks,
Steve.

Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by Franklin52; 01-19-2012 at 03:24 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 01-19-2012
Quote:
Originally Posted by steven12
Hi,

I am very new to scripting and I wanted to write a unix shell script which can give me,

1)number of cpu's in a box

2)number of cores per cpu

3)total number of cores in abox (ie multiplying 1&2)

I am also trying to figure out how to check if hyper-threading is enabled in the server and also this check to script.

intially I am looking this to run on linux then wanted to implement it on other flavours of unix.
Each unix flavor will require its own section within the script...not to mention different versions of the same unix flavor may use different commands to get the information that you are looking for...and /proc/cpuinfo is found only on linux. You need to learn some shell scripting before trying to write this sort of thing...

echo num_cpus = `grep 'physical id' /proc/cpuinfo | sort -u | wc -l`
read num_cpu

should really be...
Code:
num_cpus=`grep 'physical id' /proc/cpuinfo | sort -u | wc -l`
echo $num_cpus

# 3  
Old 01-19-2012
Hi,

Thanks for your reply.

yeah, I know that different unix flavors requires different commands but I want to start with linux.

what I am trying to do is display number of cpu and cores for each cpu and multiplying both to get total number of cores in the server.

when i change it to echo $num_cpus then where should i use the read command.

thanks,
steve.
# 4  
Old 01-19-2012
Quote:
Originally Posted by steven12
what I am trying to do is display number of cpu and cores for each cpu and multiplying both to get total number of cores in the server.
Why bother? Each core shows up individually whether it's in a separate processor or not, just count them.
Code:
awk '/^processor/ { N++} END { print N }' /proc/cpuinfo

Besides, how do you know each CPU has the same number of cores?

To extract both:

Code:
awk -F: '/^physical/ && !ID[$2] { P++; ID[$2]=1 }; /^physical/ { N++ };  END { print N, P }' /proc/cpuinfo

# 5  
Old 01-19-2012
Thanks corona,

then how do we know if Hyperthreading is enabled or not and how doo we check that?

Thanks,
Steve.
# 6  
Old 01-19-2012
I don't have a linux computer with hyperthreading to check right now, but that also shows up in /proc/cpuinfo.
# 7  
Old 01-20-2012
Hi Corona,
Code:
 
awk -F: '/^physical/ && !ID[$2] { P++; ID[$2]=1 }; /^physical/ { N++ };  END { print N, P }' /proc/cpuinfo

thanks for the above script. It shows the right information but how can we add comments to it so that we can understand what it is displaying?

eg output of above script.

16 4 -->script displays as this but how we add comments to this such that it will show

total cores =16 and cpus =4

thanks,
steve.

Last edited by vbe; 01-20-2012 at 01:34 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #838
Difficulty: Medium
Both SMTP and HTTP are valid application layer protocols used as transport for SOAP.
True or False?

10 More Discussions You Might Find Interesting

1. Solaris

Calculating number of CPUs in Solaris-8

Hello, I have few old V210/V240, running Solaris-8. I am trying to find, number of physical CPUs, number of cores and number of virtual processors. psrinfo is giving me confusing status. It looks like, there are 2 physical CPUs, but psrinfo -p shows me just 1 bash-2.03$ psrinfo -v Status of... (4 Replies)
Discussion started by: ron323232
4 Replies

2. Solaris

How to calculate total number of cores on my servers ?

Hi, I want to get total number of cores on my all non-global zones on Solaris 10. I got two methods and both are giving different results. Below link is a script, which tells me that total cores are 8 Mandalika's scratchpad: Oracle Solaris: Show Me the CPU, vCPU, Core Counts and the... (4 Replies)
Discussion started by: ron323232
4 Replies

3. Red Hat

Control Cores/CPUs used in Linux instance on VMware

We have a production setup where we have RHEL instances on VMware. We have 2 RHEL instances on VMware now each having 4 CPUs/cores each. I want to put up another RHEL instance but my query is that can we allocate only 1 CPU/core for the new instance. I hope, my query is clear that can we... (3 Replies)
Discussion started by: RHCE
3 Replies

4. UNIX and Linux Applications

Looking to reduce the number a cpus available for SGE

Hey all Im looking to reduce the number of cpus available on a certain node in our cluster available for jobs using SGE. i.e. we have one node that has 24 cpus available for jobs on SGE, i would like to reduce that to 16. Thanks (1 Reply)
Discussion started by: olifu02
1 Replies

5. Shell Programming and Scripting

Total number of users logged in a server from uptime

how to find out total number of users logged in a server from uptime . i mean to say i need the total output of unix command . who gives the out put at a particular time . I need at all time from which machine who has connected , (3 Replies)
Discussion started by: amiya.te@gmail
3 Replies

6. Shell Programming and Scripting

problem writing a simple c shell script

#!/bin/csh echo hello world this is what i got in a text file called ss1. i type "chmod 755 ss1.txt" to make it executable. then when i type ss1 or ss1.txt it says "ss1 command not found" what am i doing wrong? (19 Replies)
Discussion started by: pantelis
19 Replies

7. Shell Programming and Scripting

Help with writing simple bash script

I want to write a bash script to: 1. Send an email from localhost to an external gmail account. (gmail then automatically forwards the message back to a pop account on the same server. 2. Script waits 3 minutes then checks to see if the email arrived, and if not, it sends an email to... (9 Replies)
Discussion started by: sallyanne
9 Replies

8. Shell Programming and Scripting

Need Script to Use CPUs on a HPUX server to simulate Workload Manager on HPUX.

I am running HPUX and using WLM (workload manager). I want to write a script to fork CPUs to basically take CPUs from other servers to show that the communication is working and CPU licensing is working. Basically, I want to build a script that will use up CPU on a server. Any ideas? (2 Replies)
Discussion started by: cpolikowsky
2 Replies

9. UNIX for Dummies Questions & Answers

HELP! writing simple shell script

how would i write a shell script to show the number of lines in which int variable appears in a c++ program. (3 Replies)
Discussion started by: deadleg
3 Replies

10. UNIX for Dummies Questions & Answers

Need help writing simple script

I'm trying to write a simple unix script that will delete files after 30 days of being created. I've never done this before but conceptually it sounds easy. Here is what I'm trying to do: Get System Date Get File Date If (sysdate-filedate>30days) rm file All of these files are contained... (1 Reply)
Discussion started by: tamdoankc
1 Replies

Featured Tech Videos