Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Currently I'm using the following to check how long a process is actually running:

ps -eo uid,pid,etime | egrep '^ *MY_ID' | egrep 'PID_OF_PROCESS'

And that outputs the following:

MY_ID PID_OF_PROCESS       00:16

However, after the process ends I want to find out how long it actually ran for but I can't seem to find that information.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

I don't think it is possible to obtain such information after the process has already finished.

If you know beforehand that you will need that information, you can run the command as

time <command>

e.g.

~> time sleep 1

real        0m1.003s
user        0m0.002s
sys         0m0.001s
share|improve this answer

Check if your unix variant supports process accounting. For example, on Ubuntu (and most other Linux distributions), this is provided by the acct package Install acct. If the accounting subsystem is up and running, then lastcomm shows information about finished processes, including how much processor time they used (the wall clock time isn't recording).

If you need timing for a specific program, invoke it through time.

If you need more precise information, use a fancier auditing package, for example the audit subsystem under Linux.

share|improve this answer

Before I saw this questions and its answers, I made a little bash script. This is still useful, if you don't have root rights and acct isn't installed or you don't invoke your command directly (in my case: I use a gui to setup background processes). You need to get the PID before running (anyone interested in adding it into the script? :)

#!/bin/bash

echo "Checking run time"
read -p "What is the pid? " PID

while true; do
    # sleep needed! used to reduce cpu usage and mass of output
    sleep 5
    ps -eo uid,pid,etime | egrep '$PID' | egrep '$UID'
done

With this, find the PID of PROCESS_NAME

ps aux | awk 'NR == 1 || /PROCESS_NAME/'

Actually I tried combining USER-ID and PID to make the script non-ambigious, but it's not that easy... ps -eo uid,pid,etime | egrep '$UID[[:space:]]$PID' don't seem to work always...

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.