How to find uptime of a linux process
As «uptime» has several meanings, here is a useful command.
ps -eo pid,comm,lstart,etime,time,args
This command lists all processes with several different time-related columns. It has the following columns:
PID COMMAND STARTED ELAPSED TIME COMMAND
PID = Process ID
first COMMAND = only the command name without options and without arguments
STARTED = the absolute time the process was started
ELAPSED = elapsed time since the process was started (wall clock time), format [[dd-]hh:]mm:ss TIME = cumulative CPU time, «[dd-]hh:mm:ss» format
second COMMAND = again the command, this time with all its provided options and arguments
If you have a limited version of ps such as is found in busybox , you can get the process start time by looking at the timestamp of /proc/ . For example, if the pid you want to look at is 55.
# ls -al /proc | grep 55 dr-xr-xr-x 7 root root 0 May 21 05:53 55
. and then compare it with the current date.
# date Thu May 22 03:00:47 EDT 2014
can generate the etimes= with proc entries like etime=$(date -d «$(stat -c %y /proc/$
1234 being the process id.
example with two processes started at the same hour minute seconds but not the same milliseconds:
$ stat /proc/9355 . Access: 2017-11-13 17:46:39.778791165 +0100 Modify: 2017-11-13 17:46:39.778791165 +0100 Change: 2017-11-13 17:46:39.778791165 +0100 $ stat /proc/9209 . Access: 2017-11-13 17:46:39.621790420 +0100 Modify: 2017-11-13 17:46:39.621790420 +0100 Change: 2017-11-13 17:46:39.621790420 +0100
yes, too old and yet too hard stuff. I tried with the above proposed «stat» method but what if I had «touch»-ed the PID proc dir yesterday? This means my year-old process is shown with yesterday’s time stamp. Nah, not what I need 🙁
In the newer ones, it’s simple:
ps -o etimes -p ELAPSED 339521
as simple as that. Time is present in seconds. Do whatever you need it for. With some older boxes, situation is harder, since there’s no etimes. One could rely on:
ps -o etime -p ELAPSED 76-03:26:15
which look a «a bit» weird since it’s in dd-hh:mm:ss format. Not suitable for further calculation. I would have preferred it in seconds, hence I used this one:
ps -o etime -p --no-headers | awk -F '(:)|(-)' 'BEGIN;=1;i--) s=s+a[i]*$i>END' 339544
do not parse the output of etime because busybox 1.29.3 changed the format. use the stat + /proc method instead
Such a simple thing is not properly answered after 5 years?
I don’t think you can accurately get milliseconds. eg. if you see man procfs and see /proc/$$/stat which has field 22 as startime, which is in «clock ticks», you would have something more precise, but clock ticks aren’t going at a perfectly constant rate (relative to ‘wall clock time’) and will be off. sleeping and certain things (ntpd I guess) offset it. For example on a machine running ntpd, with 8 days uptime and has never slept, dmesg -T has the same problem (I think. ), and you can see it here:
# date; echo h > /proc/sysrq-trigger; dmesg -T | tail -n1 ; date Fri Mar 3 10:26:17 CET 2017 [Fri Mar 3 10:26:16 2017] sysrq: SysRq : HELP : loglevel(0-9) reboot(b) crash(c) terminate-all-tasks(e) memory-full-oom-kill(f) kill-all-tasks(i) thaw-filesystems(j) sak(k) show-backtrace-all-active-cpus(l) show-memory-usage(m) nice-all-RT-tasks(n) poweroff(o) show-registers(p) show-all-timers(q) unraw(r) sync(s) show-task-states(t) unmount(u) force-fb(V) show-blocked-tasks(w) Fri Mar 3 10:26:17 CET 2017
# example pid here is just your shell pid=$$ # current unix time (seconds since epoch [1970-01-01 00:00:00 UTC]) now=$(date +%s) # process start unix time (also seconds since epoch) # I'm fairly sure this is the right way to get the start time in a machine readable way (unlike ps). but could be wrong start=$(stat -c %Y /proc/"$pid") # simple subtraction (both are in UTC, so it works) age=$((now-start)) printf "that process has run for %s seconds\n" "$age"
How to get the start time of a long-running Linux process?
Is it possible to get the start time of an old running process? It seems that ps will report the date (not the time) if it wasn’t started today, and only the year if it wasn’t started this year. Is the precision lost forever for old processes?
Is there anything wrong with using ps -p
@ajwood It would be better to use ps -p
Is there anything wrong with using ps -p -o lstart ? Maybe the fact there’s no lstart neither in 2004 Edition nor in 2013 Edition of POSIX 1003.1 standard?
@PiotrDobrogost, that would be a problem if the question asked about POSIX, but it’s asking about Linux.
Mods — techraf, Makyen, David Rawson, Tsyvarev, Paul Roub — why don’t you move it to a more appropriate site such as StackExchange or Superuser instead of closing the question? This is a good and useful question
8 Answers 8
You can specify a formatter and use lstart , like this command:
The above command will output all processes, with formatters to get PID, command run, and date+time started.
Example (from Debian/Jessie command line)
$ ps -eo pid,lstart,cmd PID CMD STARTED 1 Tue Jun 7 01:29:38 2016 /sbin/init 2 Tue Jun 7 01:29:38 2016 [kthreadd] 3 Tue Jun 7 01:29:38 2016 [ksoftirqd/0] 5 Tue Jun 7 01:29:38 2016 [kworker/0:0H] 7 Tue Jun 7 01:29:38 2016 [rcu_sched] 8 Tue Jun 7 01:29:38 2016 [rcu_bh] 9 Tue Jun 7 01:29:38 2016 [migration/0] 10 Tue Jun 7 01:29:38 2016 [kdevtmpfs] 11 Tue Jun 7 01:29:38 2016 [netns] 277 Tue Jun 7 01:29:38 2016 [writeback] 279 Tue Jun 7 01:29:38 2016 [crypto] .
You can read ps ‘s manpage or check Opengroup’s page for the other formatters.
Be aware that lstart time can change, the stat methods below are safer — unix.stackexchange.com/questions/274610/….
The ps command (at least the procps version used by many Linux distributions) has a number of format fields that relate to the process start time, including lstart which always gives the full date and time the process started:
# ps -p 1 -wo pid,lstart,cmd PID STARTED CMD 1 Mon Dec 23 00:31:43 2013 /sbin/init # ps -p 1 -p $$ -wo user,pid,%cpu,%mem,vsz,rss,tty,stat,lstart,cmd USER PID %CPU %MEM VSZ RSS TT STAT STARTED CMD root 1 0.0 0.1 2800 1152 ? Ss Mon Dec 23 00:31:44 2013 /sbin/init root 5151 0.3 0.1 4732 1980 pts/2 S Sat Mar 8 16:50:47 2014 bash
(In my experience under Linux, the time stamp on the /proc/ directories seem to be related to a moment when the virtual directory was recently accessed rather than the start time of the processes:
# date; ls -ld /proc/1 /proc/$$ Sat Mar 8 17:14:21 EST 2014 dr-xr-xr-x 7 root root 0 2014-03-08 16:50 /proc/1 dr-xr-xr-x 7 root root 0 2014-03-08 16:51 /proc/5151
Note that in this case I ran a «ps -p 1» command at about 16:50, then spawned a new bash shell, then ran the «ps -p 1 -p $$» command within that shell shortly afterward. )
Get program execution time in the shell
I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution. I know I could write a perl or python script that would do this, but is there a way I can do it in the shell? (which happens to be bash)
11 Answers 11
Use the built-in time keyword:
$ help time time: time [-p] PIPELINE Execute PIPELINE and print a summary of the real time, user CPU time, and system CPU time spent executing PIPELINE when it terminates. The return status is the return status of PIPELINE. The `-p' option prints the timing summary in a slightly different format. This uses the value of the TIMEFORMAT variable as the output format.
real 0m2.009s user 0m0.000s sys 0m0.004s
How is this used on a command like time -p i=x; while read line; do x=x; done < /path/to/file.txt ? It immediatly returns 0.00 unless I don't put anything before the while loop.. what gives?
@natli: While time can time an entire pipeline as-is (by virtue of being a Bash keyword), you need to use a group command ( < . ; . ; >) to time multiple commands: time -p < i=x; while read line; do x=x; done < /path/to/file.txt; >
You can get much more detailed information than the bash built-in time (i.e time(1), which Robert Gamble mentions). Normally this is /usr/bin/time .
Editor’s note: To ensure that you’re invoking the external utility time rather than your shell’s time keyword, invoke it as /usr/bin/time . time is a POSIX-mandated utility, but the only option it is required to support is -p . Specific platforms implement specific, nonstandard extensions: -v works with GNU‘s time utility, as demonstrated below (the question is tagged linux); the BSD/macOS implementation uses -l to produce similar output — see man 1 time .
Example of verbose output:
$ /usr/bin/time -v sleep 1 Command being timed: "sleep 1" User time (seconds): 0.00 System time (seconds): 0.00 Percent of CPU this job got: 1% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.05 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 0 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 210 Voluntary context switches: 2 Involuntary context switches: 1 Swaps: 0 File system inputs: 0 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0
How can I measure the execution time of a terminal process?
I’m trying to measure the execution time of a process that I call via the command line (i.e., I want to find out how long it takes to for the process to finish). Is there any command that I can add to the command calling the process that will achieve this?
8 Answers 8
Add time before the command you want to measure. For example: time ls .
The output will look like:
real 0m0.606s user 0m0.000s sys 0m0.002s
Explanation on real , user and sys (from man time ):
- real : Elapsed real (wall clock) time used by the process, in seconds.
- user : Total number of CPU-seconds that the process used directly (in user mode), in seconds.
- sys : Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
@ninjalj, can you provide more information on what the real , user , and sys times are that this command returns?
Note that you may need sudo apt-get install time if you are using a shell where time is not a builtin.
Note that this is the output from Bash’s time builtin, but man time would be about an executable (like /usr/bin/time , from the time package), and its output would look different. Also in Bash, you can run help time for help with the builtin.
Note that the process end does not mean all the work is finished. A copy may take additional minutes after the «time» returns for flushing system bufffers (therefor with unallocated sys time also).
For a line-by-line delta measurement, try gnomon.
It is a command line utility, a bit like moreutils’s ts, to prepend timestamp information to the standard output of another command. Useful for long-running processes where you’d like a historical record of what’s taking so long.
Piping anything to gnomon will prepend a timestamp to each line, indicating how long that line was the last line in the buffer—that is, how long it took the next line to appear. By default, gnomon will display the seconds elapsed between each line, but that is configurable.