Categories

Main
Database
Java
Microsoft.NET
Adabas
DB2
Informix
Microsoft SQL Server
MySQL
Oracle
Pervasive.SQL
PostgreSQL
Sybase
Other
ASP
ColdFusion
Crystal Reports
Delphi, C etc
JAVA
Microsoft.NET
Perl and the DBI
PHP
ANSI SQL
Unix Shell Scripts
Visual Basic
XML & XSLT
Corel Paradox
FileMaker
Microsoft Access
Microsoft Excel
Other PC Databases
Applications & Tools
Database Concepts & Design
EJB programming & troubleshooting
EJB design
General J2EE
XML & Web services
Web tier: servlets, JSP, Web frameworks
Performance and scalability
Industry news
TSS feedback
Mobicents Contributors
Mobicents Users
JSLEE Resource Adaptor Types
Planning JavaOne 2008
Sun Tech Days
Other Java conferences
Binary Web Services and XML
Metro and JAXB
GlassFish
GlassFish Plugins
Project jMaki
GlassFish WebTier
Mural
Java Development Tools
Java WS & XML Community News
JAXP
Java SE
6uN Early Access
Java Quick Starter
Java SE Snapshots: Project Feedback
JCK Forum
Feedback and Suggestions
JDK Distros
JDK Open Source
General JSR Discussion
JCP 2.6
JXTA Community Forum
ME Interest
ME Cool Apps
ME General Help
ME Feedback & Suggestions
ME Application Developer Interest
Blu-ray Disc Java
ME Developer Days
Squawk
Mobile Developer Alliance
OpenCable
LWUIT
JavaFX Script Language Discussion
OpenJFX General Discussion
Scene Graph
General Performance Discussion
Your Java Career
NetBeans 6.0
Servlets
JSP
JSF
Portals and Portlets
EJB and Other Java EE Technologies
Distributed Java
Object Relational Mapping
JDBC
Web Services
Swing / AWT / SWT / JFace
JNLP and Web Start
Java Micro Edition
Sockets and Internet Protocols
Threads and Synchronization
Performance
Applets
I/O and Streams
Other Java APIs
Game Development
Java in General (beginner)
Java in General (intermediate)
Java in General (advanced)
Programmer Certification (SCJP)
Developer Certification (SCJD)
Associate Certification (SCJA)
Web Component Certification (SCWCD)
EJB Certification (SCBCD)
Mobile Application Certification (SCMAD)
Architect Certification (SCEA)
Web Services Certification (SCDJWS)
XML Certification
Product and Other Certifications
Mock Exam Errata
Sun Certification Results
Authors' Corral
Book Reviews
Events
Bunkhouse Porch
Teachers' Lounge
Testing
OO, Patterns, UML and Refactoring
IDEs, Version Control and other tools
Ant, Maven and Other Build Tools
Linux / UNIX
Mac OS
HTML and JavaScript
XML and Related Technologies
Agile and Other Processes
General Computing
Security
Groovy
Scala
Other Languages
Struts
Application Frameworks
Other Open Source Projects
BEA/Weblogic
IBM/Websphere
Oracle/OAS
Apache/Tomcat
JBoss
Other Java Products and Servers
JavaRanch
Cattle Drive (java college)
Moderators Only
Trash Can
Jobs Offered
Jobs Wanted
Jobs Discussion
Meaningless Drivel
Programming Diversions
Blatant Advertising
Java Announcements
New To Java
Advanced Java
Java Applets
Networking
Threads and Synchronization
Java 2D
AWT / Swing
SWT / JFace
CLDC and MIDP
CDC and Personal Profile
Sun Java Wireless Toolkit
Enterprise JavaBeans
JavaServer Pages (JSP) and JSTL
Java Servlet
JavaServer Faces
Web Frameworks
Database
XML
Lucene
NetBeans
Eclipse
IntelliJ IDEA
JCreator
Other IDEs
Java Tutorials
Java Tips
Jobs Discussion
Jobs Offered
Jobs Wanted
Professional Certification
Forum Lobby
Java Blogs
Introductions
Reviews / Advertising
Suggestions & Feedback

Resources

Java Database
Linux
Coding
Mobile
Hardware
Software Development
Software Development
iOS,OS X
iOS,OS X
ORACLE
IBM DEVELOPER
IBM DEVELOPER
MSDN
MSDN


Tags

Unix Shell Scripts

The database administrator's other best friend! Shell scripts can be used for many of the mundane tasks involving databases including backup automation and batch job control.

Unix Script to get the CPU statistics


Hello Experts,

I have to give the statistics of CPU every week. I am using the following:
date
ps -eo  "user, pid, stime, pcpu, pmem, etime,args"|sort -nr|egrep -e 'informat'|grep -v egrep;|cut -c1-75

This scripts runs every 15 minutes and appends the output in a log file. which looks something like:

Wed Oct  4 00:15:00 CUT 2006
informat 67808 A    0.0   0.0 16-23:16:33 pmserver pmserver_PMETACDT.cfg 
informat 66606 A    0.0   0.0 16-23:16:53 pmserver pmserver_PMETASRC.cfg 
informat 60330 A    0.0   0.0 16-23:16:10 pmserver pmserver_PMETASCOR.cfg 
informat 60068 A    0.0   0.0 16-23:17:03 pmserver pmserver_PMETASHD.cfg 
informat 59158 A    0.0   0.0 16-17:21:49 pmbwserver INFO_SPVC_CRP %PM_USER
informat 58328 A    0.0   0.0 16-23:18:48 pmrepagent Config/PMETASHD-es.cfg
informat 57932 A    0.0   0.0       00:00 /bin/ksh /informatica/pc/test.ksh
informat 54596 A    0.5  20.0    01:08:21 pmdtm -s BWLOAD:WF_SEDW_BW_VEND_A
informat 49590 A    0.0   0.0 16-23:17:15 pmserver pmserver_PMETAGLS.cfg 
informat 39850 A    0.0   0.0 16-23:16:22 pmserver pmserver_PMETASPVC.cfg 
informat 39260 A    0.0   0.0 16-23:19:04 pmrepagent Config/PMETAGLS-es.cfg
informat 36056 A    0.0   0.0 16-17:21:55 pmbwserver INFO_SEDW_CRP %PM_USER
informat 32896 A    0.0   0.0 16-23:21:55 pmrepserver pmrepserver.cfg 
informat 31904 A    0.0   0.0       00:00 ps -eo user, pid, stime, pcpu, pm
informat 30580 A    0.0   0.0 16-23:19:22 pmrepagent Config/PMETACDT-es.cfg
informat 29596 A    0.0   0.0 16-17:22:00 pmbwserver BWINFORMAT_CRP %PM_USE
informat 26646 A    0.1   0.0 16-23:18:30 pmrepagent Config/PMETASRC-es.cfg
informat  7572 A    9.5   0.0       00:58 pmdtm -s ODS:WF_STG_MOSAP_SLS.s_m

Now I want the out put in an excel file with the addition of PCPU and PCMEM for that particular time.For example ;

Date                     process name     Total_cpu_used      Date                             process name        Total memory used 
oct 04 2006 00.15     Informat                       10.1                     oct 04 2006 00.15              Informat                          20

Can any one please help me in doing this....

Thanks in advance..

~Himansu

Code:
	while : 
do
  ps -u informat -o "user,pid,stime,pcpu,pmem,etime,args" | tail +2 | cut -c1-75 | sort -nr |  while read line
  do
    printf '%s\n' "$(date '+%b %d %Y %H.%M') $line" >>logfile
  done
  sleep 900
done

Hi,

Thanks for looking into it. But still I am getting multiple values:

Oct 06 2006 13.57 informat 7987428 A    0.0   0.0       00:00 sh test.sh
Oct 06 2006 13.57 informat 7864382 A    0.0   0.0 30-10:42:26 pmrepagent Config/DMETAGLS-es.c
Oct 06 2006 13.57 informat 7651448 A    0.0   0.0       00:00 ps -u informat -o user,pid,stim
Oct 06 2006 13.57 informat 7553272 A    0.0   0.0    23:23:52 pmdtm -s CET_MAIN:wf_CET_LISTEN
Oct 06 2006 13.57 informat 7192660 A    0.0   0.0 30-10:41:04 pmrepagent Config/DMETASRC-es.c
Oct 06 2006 13.57 informat 7151840 A    0.0   0.0    22:24:17 pmdtm -s CET_MAIN:wf_CET_LISTEN
Oct 06 2006 13.57 informat 7127090 A    0.0   0.0 13-22:11:53 pmbwserver INFO_SEDW_CRD %PM_US
Oct 06 2006 13.57 informat 6930604 A    0.0   0.0 30-10:44:03 pmrepserver pmrepserver_ETD.cfg
Oct 06 2006 13.57 informat 6848626 A    0.1   1.0 30-10:42:42 pmrepagent Config/DMETACDT-es.c
Oct 06 2006 13.57 informat 6824064 A    0.0   0.0 25-01:28:02 pmserver pmserver_SMETASND.cfg
Oct 06 2006 13.57 informat 6709374 A    0.0   0.0 30-10:37:45 pmserver pmserver_DMETAGLS.cfg
Oct 06 2006 13.57 informat 6692892 A    0.0   0.0       06:04 ftpd
Oct 06 2006 13.57 informat 6504612 A    0.0   0.0       00:00 cut -c1-75
Oct 06 2006 13.57 informat 6471746 A    0.0   0.0       00:00 sort -nr
Oct 06 2006 13.57 informat 6348844 A    0.0   0.0       01:28 -csh
Oct 06 2006 13.57 informat 6308048 A    0.0   0.0       00:00 tail +2
Oct 06 2006 13.57 informat 6045948 A    0.0   0.0 16-19:38:25 pmserver pmserver_DMETASPVC.cfg
Oct 06 2006 13.57 informat 5603582 A    0.0   0.0 13-22:12:36 pmserver pmserver_DMETACDT.cfg
Oct 06 2006 13.57 informat 5349574 A    0.0   0.0 90-09:28:01 pmrepagent Config/SMETASND-es.c
Oct 06 2006 13.57 informat 5128366 A    0.0   0.0 90-10:16:29 pmrepserver pmrepserver_ETS.cfg
Oct 06 2006 13.57 informat 4784162 A    0.0   0.0 13-22:11:42 pmbwserver INFO_SPVC_CRD %PM_US
Oct 06 2006 13.57 informat 4743194 A    0.0   0.0 30-09:54:06 pmserver pmserver_DMETASCOR.cfg
Oct 06 2006 13.57 informat 4137018 A    0.0   0.0 30-10:37:22 pmserver pmserver_DMETASHD.cfg
Oct 06 2006 13.57 informat 3883256 A    0.0   0.0 30-10:36:26 pmserver pmserver_DMETASRC.cfg
Oct 06 2006 13.57 informat 3768504 A    0.0   0.0 13-22:11:47 pmbwserver BWINFORMAT_CRD %PM_U
Oct 06 2006 13.57 informat 3211382 A    0.0   0.0 30-10:42:04 pmrepagent Config/DMETASHD-es.c
Oct 06 2006 13.57 informat 1261588 A    0.0   0.0 13-22:11:37 pmbwserver INFO_SCOR_CRD %PM_US

I need the out put as:

Oct 06 2006 13.57 informat   0.1   1.0

Thanks
Himansu

OK so you want to sum the usage of all the user's processes
Note however that ps gives you a snapshot at the time of running the command and not the average since the last run.
i.e. if you take a snapshot at 10:00 when cpu usage is 0%
and between 10:01 and 10:13 you had a process using 100% cpu
your snapshot at 10:15 will again report 0% usage.
$ pcpu=$((ps -u informat -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
$ pmem=$((ps -u informat -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
$ echo "$(date '+%b %d %Y %H.%M') informat $pcpu $pmem"

Thanks! But I am still facing some problems.I am executing the following in AIX:
ps -u informat -o "user,pid,stime,pcpu,pmem,etime,args" | tail +2 | cut -c1-75 | sort -nr | 
$pcpu=$((ps -u informat -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l),$pmem=$((ps -u informat -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l) 
printf '%s\n' "$(date '+%b %d %Y %H.%M') $line" >>logfile
$echo "$(date '+%b %d %Y %H.%M') informat $pcpu $pmem"

It is doing the summation but the $pcpu $pmem are not getting appended into the logfile.The following message is getting displayed on my screen:
[288] % sh test1.sh
test1.sh[2]: =0.1,=0:  not found.
test1.sh[4]: Nov 02 2006 16.18 informat  :  not found.

Thanks
Himansu






;














;

OK lets analyze this command

	Quote:
	
	
		
			
				Originally Posted by pintu005
				$echo "$(date '+%b %d %Y %H.%M') informat $pcpu $pmem"
			
		
	
	

The shell substitute all the variables
Assuming the variable $echo is not set it will translate to ''
next the date command $(....) will return a date 'Nov 02...' , etc,etc
Now the shell try and execute the line that start with Nov
i.e. trying to run the command Nov... that doesn't exist
Hence the error: not found
Remove the $ in front of echo (or replace with the more portable printf)


	Quote:
	
	
		
			
				Originally Posted by pintu005
				$pcpu $pmem are not getting appended into the logfile
			
		
	
	
You did not include them in your 'printf....>>logfile' command. Change to 
printf '%s\n' "$(date '+%b %d %Y %H.%M') $pcpu $pmem" >>logfile

You need to drop the initial $ in front of the commands that I previously posted (post #4). I copied and pasted from a terminal session where the $ is the command prompt. Sorry for the confusion.

PS. I did not test on AIX but it should work there.

Thanks! So much. It's working now.

Sorry for troubling again. I have modified the script as no I have to give stats for some more users. The script is 
***********
#!/bin/sh
pcpuinformat=$((ps -u informat -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmeminformat=$((ps -u informat -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcpudb2eod=$((ps -u db2eod -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemdb2eod=$((ps -u db2eod -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcpudb2emd=$((ps -u db2emd -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemdb2emd=$((ps -u db2emd -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcputrillium=$((ps -u trillium -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemtrillium=$((ps -u trillium -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcpuinfouser=$((ps -u infouser -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmeminfouser=$((ps -u infouser -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcputriluser=$((ps -u triluser -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemtriluser=$((ps -u triluser -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcpuroot=$((ps -u root -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemroot=$((ps -u root -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
  
echo "$(date '+%b %d %Y %H.%M') informat $pcpuinformat $pmeminformat db2eod $pcpudb2eod $pmemdb2eod db2emd $pcpudb2emd $pmemdb2emd trillium $pcputrillium $pmemtrillium infouser $pcpuinfouser $pmeminfouser triluser $pcputriluser $pmemtriluser root $pcpuroot $pmemroot"  ## >> /informatica/pc/logfile1 

***********
i have scheduled this script in crontab.Now I am getting syntax error in the output file.Below is the output file.

**********************************
Dec 20 2006 20.26 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.27 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.28 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.29 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
syntax error on line 1 stdin
Dec 20 2006 20.30 informat 0.2  db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.31 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.32 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.33 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.34 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.35 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
syntax error on line 1 stdin
Dec 20 2006 20.36 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root  1.0
Dec 20 2006 20.37 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.38 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.39 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
syntax error on line 1 stdin
syntax error on line 1 stdin
Dec 20 2006 20.40 informat 0.2  db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root  1.0
Dec 20 2006 20.41 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.42 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.43 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.44 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
Dec 20 2006 20.45 informat 0.2 0 db2eod  0 db2emd 0.1  trillium 0 0 infouser 0 0 triluser 0 0 root
syntax error on line 1 stdin
Dec 20 2006 20.46 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0
Dec 20 2006 20.47 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0

**********************************
If we analyze the output, we will see whenever it is not finding any value for any user like for example it's not finding a value for pcpuformat or pmeminformat or pcpuroot or pmemroot.....it's throwing a syntax error.

Please advice,how can I avoid this.

Thanks.
Waiting for your sugestion.

Try:
	Code:
	pcpuinformat=$((ps -u informat -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l 2>/dev/null) etc...


Related Links

entire line in a variable
Shell Script help needed
truss: non-existent process ignored:
Find and replace in unix
pointers in SHELL
How to un tar a specific file with the same name but diff date?
multiple cuts in a line
value present in a list
Alternative to uniq
passing * in unix
Text formating
More Newbness
insert to an empty file
awk question
ftp
String function