Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am writing a shell script that gets all the current processes running, and pipes them into grep to filter out the ones I need.

But I also need it to give me a timestamp, either when the ps occurs, or when the process was started. It doesn't matter, I just need a time. All the processes just take fractions of a second.

My command is this:

ps -U USERNAME -o rss=MEM,comm=CMD,pid=PID

I tried going like

ps -U USERNAME -o rss=MEM,comm=CMD,pid=PID, start=START

but that just gives me a time like hh:mm:ss. I need something more precise than this. Timestamps are ideal.

What should I do?

share|improve this question

2 Answers 2

Since you need more precise time than seconds, you can either generate your own timestamp with:

date +%Y%m%d%H%M%s.%N

keep in mind that date itself takes ~.001s to run (dependent on system), so the nanoseconds may not be the right number of significant digits.

or you can look in /proc/$THIS_PID/stat using stat to see the start time of /init we can use:

stat -c %x /proc/1/stat

%x   Time of last access
%X   Time of last access as seconds since Epoch
%y   Time of last modification
%Y   Time of last modification as seconds since Epoch
%z   Time of last change
%Z   Time of last change as seconds since Epoch

2012-03-10 12:18:45.328784363 +0800

though if the processes are really fast it it may be missing by the time you get to it, this would be the case for other solutions as well, but you may want to check that the file exists first

share|improve this answer

Try using ps -ef this should give the start date and time

share|improve this answer

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.