cancel
Showing results for 
Search instead for 
Did you mean: 

date increment in file

SOLVED
Go to solution
Jim Gerken
Occasional Advisor

date increment in file

I put he following into a file called "CPU"

echo "PID USER CPU% MEM_SIZE COMMAND"
while true
do
UNIX95= ps -eo "pid ruser pcpu vsz=Kbytes" -o comm | grep oracleD5751
sleep 10
done | sed 's;^;'$(date +'%m%d%H%M%S')' ;'

I execute this file to put the contents into a log.

date > cpu.log ; cpu >> /tmp/junk/cpu.log

However the date time stamp on the output never increments. It prints the first date time and then repeats it with each interation? Any thoughts...
6 REPLIES
Sridhar Bhaskarla
Honored Contributor

Re: date increment in file

Hi,

Until your while statement is completed, your sed won't get executed?.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: date increment in file

You need to put the date command inside the loop.
If it ain't broke, I can fix that.
Jim Gerken
Occasional Advisor

Re: date increment in file

Any thoughts on how I can change things around so the time increments each time the statement runs??
James R. Ferguson
Acclaimed Contributor
Solution

Re: date increment in file

Hi:

You can achieve this with:

#!/usr/bin/sh
echo "PID USER CPU% MEM_SIZE COMMAND"
while true
do
UNIX95= ps -eo "pid ruser pcpu vsz=Kbytes" -o comm | (echo `date +'%m%d%H%M%S'`;grep vhand)|xargs
sleep 10
done

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: date increment in file

Hi (again):

Sorry, substitute 'oracleD5751' for 'vhand' [which I used to test :-)]

#!/usr/bin/sh
echo "PID USER CPU% MEM_SIZE COMMAND"
while true
do
UNIX95= ps -eo "pid ruser pcpu vsz=Kbytes" -o comm |(echo `date +'%m%d%H%M%S'`;grep oracleD5751)|xargs
sleep 10
done

Regards!

...JRF...

Sridhar Bhaskarla
Honored Contributor

Re: date increment in file

Hi,

I would do this

FILE=/tmp/junk/cpu.log
echo "DATE PID USER CPU% MEM_SIZE COMMAND" > $FILE
while true
do
printf "$(date +'%m%d%H%M%S') " >> $FILE
UNIX95= ps -eo "pid ruser pcpu vsz=Kbytes" -o comm | grep oracleD5751 >> $FILE
sleep 2
done


-Sri
You may be disappointed if you fail, but you are doomed if you don't try