# Cpu utilization script

**URL:** <https://community.unix.com/t/cpu-utilization-script/332147>\
**Category:** Shell Programming and Scripting\
**Created:** [July 23, 2013, 4:42am UTC](https://community.unix.com/t/cpu-utilization-script/332147 "2013-07-23T04:42:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NarayanaPrakash](https://community.unix.com/letter_avatar/narayanaprakash/32/6_449c533f586939b6e5f246632a46421f.png) [@NarayanaPrakash](https://community.unix.com/u/NarayanaPrakash)\
**Post date:** [July 23, 2013, 4:42am UTC](https://community.unix.com/t/cpu-utilization-script/332147/1 "2013-07-23T04:42:09Z")

</div>

I have a set of requirement

1)cpu utilization on a server with pid and and process name.  
It should display the above and also the script should have an option to record the cpu utilization for a given period of time

first part i can get with below commands

```nohighlight
ps -eo pcpu,pid,user,args or top -n 1 -b

```

how do i record if the user who runs the script want to do it for 5 minutes.

---

<div class="post-metadata">

**Author:** ![vidyadhar85](https://community.unix.com/user_avatar/community.unix.com/vidyadhar85/32/997_2.png) [@vidyadhar85](https://community.unix.com/u/vidyadhar85)\
**Post date:** [July 23, 2013, 4:49am UTC](https://community.unix.com/t/cpu-utilization-script/332147/2 "2013-07-23T04:49:23Z")

</div>

redirect the output of your top command to a file and start that as background process run sleep for 5 mins and then kill the background process.

---------- Post updated at 02:19 PM ---------- Previous update was at 02:17 PM ----------

If you have SAR utility your life would be easy

---

<div class="post-metadata">

**Author:** ![Skrynesaver](https://community.unix.com/user_avatar/community.unix.com/skrynesaver/32/14143_2.png) [@Skrynesaver](https://community.unix.com/u/Skrynesaver)\
**Post date:** [July 23, 2013, 4:49am UTC](https://community.unix.com/t/cpu-utilization-script/332147/3 "2013-07-23T04:49:59Z")

</div>

GNU `top` takes

- a -n flag which is the number of iterations to run
- a -d flag, which is the delay between iterations
- and a -b flag which streams output suitable for processing

So you could take the user input, set the delay as that interval and iterations to 2, then only use the cpu usage values in the second iteration

---

<div class="post-metadata">

**Author:** ![NarayanaPrakash](https://community.unix.com/letter_avatar/narayanaprakash/32/6_449c533f586939b6e5f246632a46421f.png) [@NarayanaPrakash](https://community.unix.com/u/NarayanaPrakash)\
**Post date:** [July 23, 2013, 5:58am UTC](https://community.unix.com/t/cpu-utilization-script/332147/4 "2013-07-23T05:58:00Z")

</div>

So will this work?

```nohighlight
top -b > ~/op.txt &
sleep $seconds
kill $!

```

---

<div class="post-metadata">

**Author:** ![Skrynesaver](https://community.unix.com/user_avatar/community.unix.com/skrynesaver/32/14143_2.png) [@Skrynesaver](https://community.unix.com/u/Skrynesaver)\
**Post date:** [July 23, 2013, 10:08am UTC](https://community.unix.com/t/cpu-utilization-script/332147/5 "2013-07-23T10:08:06Z")

</div>

It will, but you will have to parse and aggregate the resulting data, however why not

```nohighlight
top -b -d $seconds -n2 | egrep '^Cpu' | tail -1

```

Which will give you the cpu usage line.

alternatively:

```nohighlight
top -b -d $seconds -n2 > top.tmp
split -a1 -l$(( $(wc -l top.tmp| cut -d\ -f1 ) / 2 )) top.tmp top.
cat top.b
rm top.tmp top.a top.b top.c # top.c will be created with a newline character.

```

For the averages over $seconds for all processes

---

<div class="post-metadata">

**Author:** ![NarayanaPrakash](https://community.unix.com/letter_avatar/narayanaprakash/32/6_449c533f586939b6e5f246632a46421f.png) [@NarayanaPrakash](https://community.unix.com/u/NarayanaPrakash)\
**Post date:** [July 24, 2013, 1:11am UTC](https://community.unix.com/t/cpu-utilization-script/332147/6 "2013-07-24T01:11:10Z")

</div>

thank you Skrynesaver
