Linux process system time

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

Читайте также:  Linux grep pipe grep

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/$ | cut -d ‘ ‘ -f 1,2)» +%s); echo «$(date +%s) — $» | bc -l

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 Check Execution Time of a Process in Linux

linux process time

Have you been wondering how you can find an execution time of a process in a Linux or Unix system? This guide will show you a number of tools that comes in handy when trying to find the execution time of a process in Linux.

At times you may have to work on slow executing processes or having slow internet or running a program that you need to track its execution time. Let’s look at the top tools that you should try out for this.

Check Linux process time using Gnomon

Gnomon is a utility used to annotate console logging statements with timestamps and find slow processes on a Linux system. This tool is useful for long-running processes where you’d like a historical record of what’s taking so long.

Installing Gnomon

Since Gnomon is a tool written in Node.js, you need Node.js installed on your system so that you can install gnomon with npm package manager. Once you have npm tool present on your Linux system, then proceed to install them using:

$ npm install -g gnomon /usr/local/bin/gnomon -> /usr/local/lib/node_modules/gnomon/bin/gnomon + [email protected] added 56 packages in 13.076s

Using Gnomon

To prepend a timestamp to each line, you need to pipe the command to gnomon. It will indicate how long the process took to execute. By default, gnomon will display the seconds elapsed between each line, but that is configurable.

Take a look at the below example which prints the time taken to do 5 times ping request to google DNS server.

$ ping -c 5 8.8.8.8 | gnomon 0.0049s PING 8.8.8.8 (8.8.8.8): 56 data bytes 0.3603s 64 bytes from 8.8.8.8: icmp_seq=0 ttl=59 time=179.114 ms 1.0025s 64 bytes from 8.8.8.8: icmp_seq=1 ttl=59 time=182.345 ms 1.0008s 64 bytes from 8.8.8.8: icmp_seq=2 ttl=59 time=183.636 ms 1.0119s 64 bytes from 8.8.8.8: icmp_seq=3 ttl=59 time=181.139 ms 0.0002s 64 bytes from 8.8.8.8: icmp_seq=4 ttl=59 time=190.970 ms 0.0002s 0.0001s --- 8.8.8.8 ping statistics --- 0.0001s 5 packets transmitted, 5 packets received, 0.0% packet loss 0.0020s round-trip min/avg/max/stddev = 179.114/183.441/190.970/4.048 ms 0.0002s Total 3.3842s

The total time elapsed is 3.3842s.

Available Options are:

Below is a list of options available:

Type of timestamp to display.
elapsed-line: Number of seconds that displayed line was the last line.
elapsed-total: Number of seconds since the start of the process.
absolute: An absolute timestamp in UTC.

$ ping -c 3 8.8.8.8 | gnomon --type=elapsed-total 0.0049s PING 8.8.8.8 (8.8.8.8): 56 data bytes 0.2336s 64 bytes from 8.8.8.8: icmp_seq=0 ttl=59 time=46.288 ms 1.2798s 64 bytes from 8.8.8.8: icmp_seq=1 ttl=59 time=35.811 ms 1.2801s 64 bytes from 8.8.8.8: icmp_seq=2 ttl=59 time=80.783 ms 1.2802s 1.2804s --- 8.8.8.8 ping statistics --- 1.2805s 3 packets transmitted, 3 packets received, 0.0% packet loss 1.2821s round-trip min/avg/max/stddev = 35.811/54.294/80.783/19.213 ms 1.2823s Total 1.2824s

-f | —format=»format»: Format the absolute timestamp, using PHP date format strings. If the type is elapsed-line or elapsed-total, this option is ignored. The default format is «H:i:s.u O«

Читайте также:  Alt linux canon lbp 2900

Example to disable real-time update:

# ping -c 3 8.8.8.8 | gnomon --real-time=false 0.0040s PING 8.8.8.8 (8.8.8.8): 56 data bytes 0.7847s 64 bytes from 8.8.8.8: icmp_seq=0 ttl=59 time=69.803 ms 0.9316s 64 bytes from 8.8.8.8: icmp_seq=1 ttl=59 time=140.597 ms 0.0001s 64 bytes from 8.8.8.8: icmp_seq=2 ttl=59 time=68.122 ms 0.0001s 0.0001s --- 8.8.8.8 ping statistics --- 0.0001s 3 packets transmitted, 3 packets received, 0.0% packet loss 0.0020s round-trip min/avg/max/stddev = 68.122/92.841/140.597/33.776 ms Total 1.7229s

-h | —high=seconds : High threshold

-m | —medium=seconds : Medium threshold. Works just like the high threshold described above, but colors the timestamp bright instead.

Check running process time using ps

You can use ps command to check the time a particular process has been running. You need to first find process ID then use it to find elapsed time.

To identify process ID, you can use a tool like pidof

Then use ps with options -o etime to find elapsed running time.

$ ps -p 1388 -o etime ELAPSED 05-11:03:02

etime option displays elapsed time since the process was started, in the form [[DD-]hh:]mm: ss. So from above example, the process has been running for 5 days, 11 hours and 3 minutes. Use etimes option to get elapsed time in seconds.

This command option can also be used for multiple processes. The example below will display start time and the execution time of all processes on my Ubuntu server.

PID —> ID of the running process

STARTED —> The time the process was initially started

ELAPSED —> Total running time of the process

COMMAND —> Process executed command

Using time command on Ubuntu

The time command reports how long the command took to execute on a Linux system. You can install it if missing on Ubuntu system using:

time command Usage:

The output of time will have:

  • The elapsed real time between command invocation and termination.
  • The user CPU time.
  • The system CPU time.

Consider the below example to check disk usage of /root directory.

# time du -sh /root/ 464K /root/ real 0m0.007s user 0m0.002s

From the output, the actual time the command took to execute is 0m0.007s.

Let’s do one more, a ping to 8.8.8.8

# time ping -c 3 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=60 time=7.28 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=60 time=11.9 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=60 time=7.54 ms --- 8.8.8.8 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 7.281/8.925/11.952/2.145 ms real 0m2.059s user 0m0.001s sys 0m

The actual execution time is 2.059 seconds.

Conclusion

Now you know how to get the Linux Process execution time on Linux. The first method is ideal for interactive processes. For processes that run in the background, you can always get their execution time using ps command.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector