Process running time in linux

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

Читайте также:  Kali linux топ инструменты

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" 

Источник

3 Ways to check process running time in Linux

In this blog post, we will discuss three different ways to check the running time of a process in Linux. This is a useful tool for troubleshooting and determining how long specific processes are taking to run. We will also discuss some of the benefits of each method. Let’s get started!

Check process running time with ps command in Linux

The best way to check process running time in Linux is using ps command. Open the terminal and type ps -p pid -o etime,etimes. It will list the process running time.

Читайте также:  M2crypto python install kali linux

The ps command can be used to view information about all running processes. To use this command, simply type “ps aux” into your terminal. You will then see a list of all running processes, as well as their PID (process ID), and the amount of time they have been running.

  • etime Display elapsed time since the process was started, in the form [[DD-]hh:]mm:ss.
  • etimes Display elapsed time since the process was started, in seconds.

You need to pass the -o etimes or -o etime to the ps command. The syntax is:

let us find and print the PID creation date. In other words find out when the process was started on Linux, enter:

  • $ sudo ps -p -o start,etimes,etime
  • $ sudo ps -p -o start,etimes,etime
  • $ sudo ps -p -o pid,cmd,start,etimes,etime
  • $ sudo ps -C -o pid,cmd,start,etimes,etime

Check process CPU time using top command in Linux

The second method is to use the “top” command. This command provides a real-time view of all running processes. It also displays a variety of other information about each process, such as CPU usage and memory usage. To use the top command, simply type “top” into your terminal. You will then see a list of all running processes, sorted by CPU usage.

Top command shows you the total CPU time the task has used since it started. But it doesn’t include the elapsed time. So, don’t get confused between top and ps commands.

Check process running time using proc filesystem in Linux

However, the above command doesn’t show you the exact start time of the process and use the following format to check that. The proc filesystem (procfs) is a special filesystem in Unix-like operating systems that presents information about processes and other system information.

It’s sometimes referred to as a process information pseudo-file system. It doesn’t contain ‘real’ files but run time system information (e.g. system memory, devices mounted, hardware configuration, etc).

# ls -ld /proc/16337
dr-xr-xr-x. 9 root root 0 Aug 5 17:20 /proc/16337/

Both of these methods have their own benefits. The “ps” command is great for getting a quick overview of all running processes. The “top” command provides real-time information about each process, but it can be a bit overwhelming to look at.

Whichever method you choose, checking the running time of processes can be a valuable tool for troubleshooting and performance analysis. Try out both methods and see which one works best for you!

Читайте также:  Экранная клавиатура astra linux

We hope this blog post has been helpful. If you have any questions or comments, please feel free to reach out to us! We would be happy to help. Thanks for reading!

Источник

How to check how long a process has been running?

On Linux with the ps from procps(-ng) (and most other systems since this is specified by POSIX):

Where $$ is the PID of the process you want to check. This will return the elapsed time in the format [[dd-]hh:]mm:ss .

Using -o etime tells ps that you just want the elapsed time field, and the = at the end of that suppresses the header (without, you get a line which says ELAPSED and then the time on the next line; with, you get just one line with the time).

Or, with newer versions of the procps-ng tool suite (3.3.0 or above) on Linux or on FreeBSD 9.0 or above (and possibly others), use:

(with an added s ) to get time formatted just as seconds, which is more useful in scripts.

On Linux, the ps program gets this from /proc/$$/stat , where one of the fields (see man proc ) is process start time. This is, unfortunately, specified to be the time in jiffies (an arbitrary time counter used in the Linux kernel) since the system boot. So you have to determine the time at which the system booted (from /proc/stat ), the number of jiffies per second on this system, and then do the math to get the elapsed time in a useful format.

It turns out to be ridiculously complicated to find the value of HZ (that is, jiffies per second). From comments in sysinfo.c in the procps package, one can A) include the kernel header file and recompile if a different kernel is used, B) use the posix sysconf() function, which, sadly, uses a hard-coded value compiled into the C library, or C) ask the kernel, but there’s no official interface to doing that. So, the ps code includes a series of kludges by which it determines the correct value. Wow.

So it’s convenient that ps does that all for you. 🙂

Источник

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