Get process time linux

How to Check Execution Time of a Process in Linux

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

At times you may have to work on slow executing processes or having a 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. Every command shown in this guide has been tested on Ubuntu 16.04 server and on CentOS 7.

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:

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 below example which prints the time taken to do 5 times ping request to google DNS server.

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.

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

Example to disable real-time update:

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

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.

The output has 4 columns:

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.
Читайте также:  Open qnap disk in windows

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

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

The actual execution time is 2.059 seconds.

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.

Источник

Find The Execution Time Of A Command Or Process In Linux

You probably know the start time of a command/process and how long a process is running in Linux and Unix systems. But, how do you know when did the command or process end and/or what is the total time taken by the command/process to complete? Well, It’s easy! There is a utility named ‘GNU time’ that is specifically designed to measure the total execution time of a command or program. Using Time utility, we can easily find the execution time of a command or process in Linux and Unix operating systems. Good thing is ‘time’ command comes preinstalled in most Linux distributions, so you don’t have to bother with installation.

Find The Execution Time Of A Command Or Process In Linux

To measure the execution time of a command/program, just run.

The above commands displays the total execution time of ‘ls’ command. Replace «ls» with any command/process of your choice to find the total execution time.

  1. real -refers the total time taken by command/program,
  2. user — refers the time taken by the program in user mode,
  3. sys — refers the time taken by the program in kernel mode.

We can also limit the command to run only for a certain time as well. Refer the following guide for more details.

time vs /usr/bin/time

As you may noticed, we used two commands ‘time’ and ‘/usr/bin/time’ in the above examples. So, you might be wondering what is the difference between them.

First, let us see what actually ‘time’ is using ‘type’ command. For those who don’t know, the Type command is used to find out the information about a Linux command. For more details, refer this guide.

As you see in the above output, time is both,

  • A keyword built into the BASH shell
  • An executable file i.e /usr/bin/time

Since shell keywords take precedence over executable files, when you just run time command without full path, you run a built-in shell command. But, When you run /usr/bin/time, you run a real GNU time program. So, in order to access the real command, you may need to specify its explicit path. Clear? Good!

The built-in ‘time’ shell keyword is available in most shells like BASH, ZSH, CSH, KSH, TCSH etc. The ‘time’ shell keyword has less options than the executables. The only option you can use in ‘time’ keyword is -p.

You know now how to find the total execution time of a given command/process using ‘time’ command. Want to know little bit more about ‘GNU time’ utility? Read on!

A brief introduction about ‘GNU time’ program

The GNU time program runs a command/program with given arguments and summarizes the system resource usage as standard output after the command is completed. Unlike the ‘time’ keyword, the GNU time program not just displays the time used by the command/process, but also other resources like memory, I/O and IPC calls.

The typical syntax of the Time command is:

The ‘options’ in the above syntax refers a set of flags that can be used with time command to perform a particular functionality. The list of available options are given below.

  • -f, —format — Use this option to specify the format of output as you wish.
  • -p, —portability — Use the portable output format.
  • -o file, —output=FILE — Writes the output to FILE instead of displaying as standard output.
  • -a, —append — Append the output to the FILE instead of overwriting it.
  • -v, —verbose — This option displays the detailed description of the output of the ‘time’ utility.
  • —quiet — This option prevents the time ‘time’ utility to report the status of the program.
Читайте также:  Заставки время для mac os

When using ‘GNU time’ program without any options, you will see output something like below.

If you run the same command with the shell built-in keyword ‘time’, the output would be bit different:

Some times, you might want to write the system resource usage output to a file rather than displaying in the Terminal. To do so, use -o flag like below.

As you can see in the output, Time utility doesn’t display the output. Because, we write the output to a file named file.txt. Let us have a look at this file:

When you use -o flag, if there is no file named ‘file.txt’, it will create and write the output in it. If the file.txt is already present, it will overwrite its content.

You can also append output to the file instead of overwriting it using -a flag.

The -f flag allows the users to control the format of the output as per his/her liking. Say for example, the following command displays output of ‘ls’ command and shows just the user, system, and total time.

Please be mindful that the built-in shell command ‘time’ doesn’t support all features of GNU time program.

For more details about GNU time utility, refer the man pages.

To know more about Bash built-in ‘Time’ keyword, run:

Источник

Linux Commando

Initially a Linux command-line interface blog, it has evolved to cover increasingly more GUI app topics. Instead of just giving you information like some man page, I illustrate their usage in real-life scenarios.

Search This Blog

Sunday, September 21, 2008

How to get the process start date and time

How can we determine when a running process was started?

The venerable ps command deserves first consideration.

Most Linux command-line users are familiar with either the standard UNIX notation or the BSD notation when it comes to specifying ps options.

If ps -ef is what you use, that is the UNIX notation.

The STIME column displays the start time or date. From the above, we can tell that process 4901 (emacs) began execution at 16:34 (4:34 pm). But on what day, today?

From the ps man page: ‘Only the year will be displayed if the process was not started the same year ps was invoked, or «mmmdd» if it was not started the same day, or «HH:MM» otherwise.’

So, emacs was started at 16:34 TODAY.

What is the start time for the other process, the firefox process with pid 1218?

The STIME for process 1218 reads Sep20 (which was yesterday). But what time yesterday?

The default ps -ef only tells you the start date but NOT the time if a process was NOT started on the same day.

If the BSD notation is more familiar to you, you will find that ps aux yields similar results.

The Start column in the above output only reveals the start date if the process is older than the current day.

There are (at least) 2 ways to determine the exact start time if the process was started before the current day.

Solution 1
Specify elapsed time in the ps output format.

The above ps command specifies 3 fields to be included in the output: the process pid, the command, and the elapsed time, respectively.

etime is the elapsed time since the process was started, in the form dd-hh:mm:ss. dd is the number of days; hh , the number of hours; mm , the number of minutes; ss , the number of seconds.

The firefox command started execution 2 days, 16 hours, 4 minutes and 45 seconds ago. To find the exact time, you need to do some simple math.

If you prefer the BSD notation, issue this command:

Solution 2
Get the process pid and read off the timestamp in the corresponding subdirectory in /proc .

Читайте также:  Kali linux last version

First, get the process pid using the ps command ( ps -ef or ps aux )

Then, use the ls command to display the creation timestamp of the directory.

You can tell from the timestamp that the process 1218 began executing on Sept 20, 16:14.

If you can think of another clever way to get the start time and date, please let us know.

19 comments:

This post was really useful to me.
Thanks for sharing your knowledge.

Greetings from Argentina,

another simple way to know process start time:

ps -eo pid,lstart,cmd

January 29, 2009 at 10:49 PM Anonymous said.

Looking at the mtime of /proc/PID may not yield useful results. For example on my PC most entries changed their time today at 19:29 for some reason.
But using the elapsed time still works — or the lstart format option to ps as sujeet suggested.

March 18, 2009 at 12:27 PM Vincent Reydet said.

If you want the timestamp :

date -d «`ps -p __PID__ -o lstart=`» +’%s’

another simple way to know process start time:

ps -eo pid,lstart,cmd»

lol! indeed sujeet suggestion is complete and excelent! look at this output:

ps -eo pid,lstart,cmd | grep ps
1418 Mon Jun 1 15:49:19 2009 [khpsbpkt]
1454 Mon Jun 1 15:49:19 2009 [kpsmoused]
12547 Mon Jun 1 15:50:02 2009 dcopserver [kdeinit] —nosid
16642 Sun Jun 7 03:50:58 2009 ps -eo pid,lstart,cmd
16643 Sun Jun 7 03:50:58 2009 grep —colour=auto ps

June 6, 2009 at 5:54 PM Anonymous said.

actually, using ‘ps’ on linux is not always reliable. i’ve seen situations where ‘ps’ can return different start time for a given process with subsequent invocations. the difference is usually 1 second (probably a rounding error). it’s more reliable to use

proc /proc//stat | awk ‘
this will give start time in jiffies since last system reboot.

The process start time drift you’ve seen in ps is probably related to this:
https://bugzilla.redhat.com/show_bug.cgi?id=518730

Due to the way that the start time of a process is calculated by ps (/proc/stat btime + /proc/PID/stat jiffies since process start) when NTP adjusts the system clock forward over time the start time of long running processes drifts.

The timestamp of the /proc/pid file is changed under certain conditions. Example: using Ubuntu 10.4 the timestamps are updated once a day by some cron job. It’s possible to change the timestamp by just ‘touching’ the directory.

+1 for @sujeet solution

i want to display the process created date & time only in Unix ?

Anyone Know Just Give post your answer

Thanks And Regards

Great post. Thanks especially for the /proc/pid

May 10, 2013 at 2:08 AM Gabriel said.

Great post , It really helped me , thank you

Great bro, your post helped me

February 3, 2014 at 2:52 PM Kapil Awadhwal said.

Thank you sooo much for this informative post.

You can achieve the same with: ps axo pid,user,cmd,lstart

Thank you for this post, I’ve got what I needed, but Solution #2 is not a solution actually, because it sometimes gives wrong results:

$ ls -ld —full-time /proc/23555
dr-xr-xr-x 8 irv tenso 0 2016-02-04 12:00:55.005137889 +0300 /proc/23555/
$ ps -eo pid,lstart,cmd | grep 23555
23555 Thu Feb 4 12:00:00 2016 ffmpeg -i rtsp://10.2.8.2:554/axis-media/media.amp -r 30 -c copy -t 3616 -strftime 1 video//10.2.8.2_2016.02.04_12:00:00.mp4

I suggest you to remove it.

The solution with the proc directory is wrong because it’s updated frequently.

You can calculate the start time by the (already mentioned) jiffies since boot time.
If you want an easy-to-use binary for this purpose you can use «lps» of the package ngtx:
http://www.tuxad.com/download-ngtx.html

lps prints the «age» of processes at the start of the line to give you the ability of using «sort -n» to order processes by «age».

Thanks for the topic. very much useful for beginners..
i used this commands in my development phase. after reading this.

August 31, 2017 at 2:10 AM Anonymous said.

Cool info! Too bad elapsed isn’t a default column . nice!

Источник

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