16

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.

0

3 Answers 3

15

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
7

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 http://bit.ly/software-small. 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.

2
  • This answer has lost its image.
    – mattdm
    Commented Mar 7, 2016 at 21:45
  • @mattdm Oh, it's the link that was standard on Ask Ubuntu for linking to apt.ubuntu.com, and then the image got deleted from imgur. I must have used it hundreds of times across SE and I do not relish the prospect of editing them all. I'm still holding out for SE doing an automated replacement, which they won't do since they didn't even do it when all the Java documentation moved. Commented Mar 7, 2016 at 22:01
1

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...

1
  • Just what I needed! I found that on Centos 6.8 I needed to omit the quotes around '$PID' and skip egrep '$UID' as I am the only user like so: ps -eo uid,pid,etime | egrep $PID Commented Nov 19, 2018 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .