98

How do I find the uptime of a given linux process.

ps aux | grep gedit | grep -v grep

gives me a whole lot of information which includes the time at which the process was started. I am specifically looking for switch which returns the uptime of a process in milliseconds.

Thanks

9 Answers9

158

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

Abdull
  • 2,432
20

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/<PID>. 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
13

I think you can just run:

$ stat /proc/1234

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
7

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 <PID>
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 <PID>
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 <PID> --no-headers | awk -F '(:)|(-)' 'BEGIN{a[4]=1;a[3]=60;a[2]=3600;a[1]=86400;s=0};{for (i=NF;i>=1;i--) s=s+a[i]*$i}END{print s}'
339544
George Ivanov
  • 171
  • 1
  • 1
5

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

Here's seconds:

# 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"
Peter
  • 634
4

By process name:

ps -eo pid,comm,lstart,etime,args | grep MyProcessName | cut -b 1-200

Where:

  • MyProcessName is the process name.
  • The ps lists all processes.
  • The grep filters by MyProcessName in args.
  • The cut lists the first 200 characters on each line. Useful, as often Java command lines are rather long.

Produces something like this:

 10673 java            Tue Aug 25 12:26:30 2020    19:19:25 /opt/apps/java_home/bin/java -Dservice.name=MyProcessName1
 10908 java            Tue Aug 25 12:26:41 2020    19:19:14 /opt/apps/java_home/bin/java -Dservice.name=MyProcessName2
 11062 java            Tue Aug 25 12:26:52 2020    19:19:03 /opt/apps/java_home/bin/java -Dservice.name=MyProcessName3

We can see that all of the services started on August 26th at 12:26, and none of them have restarted for any reason.

Contango
  • 1,207
3

as Systemd got introduced, people who search for this with SystemD distros can use systemctl status option. And get the uptime in the output as depicted here:

● openkm.service - LSB: Start and stop OpenKM
   Loaded: loaded (/etc/rc.d/init.d/openkm; bad; vendor preset: disabled)
   Active: active (exited) since Tue 2021-04-20 09:27:13 CAT; 2 weeks 4 days ago
Shnbook
  • 31
0

It may be too old thread but still providing a solution for the following scenario-

  • ps -ef is not available (in busybox)
  • stat /proc/{pid} or ls -l /proc/{pid} might give wrong result for the processes that started before timesync and if hwclock is not up to date. For example, if current date is 2025 and if hwclock is in 2024, then for the processes that started before timesync, stat will show the file was created at 2024 and we will eventually get the uptime as 1 year but actually it is not.

Solution:

  1. cat /proc/{pid}/stat can give the stat of the process and we can get the start time in clock tick after the system boot. https://man7.org/linux/man-pages/man5/proc_pid_stat.5.html
  2. As mentioned in the man page, we can divide it by tick per second i.e divide by sysconf(_SC_CLK_TCK)
  3. We can get the uptime of the whole system by reading /proc/uptime. The first number is the uptime in seconds. https://man7.org/linux/man-pages/man5/proc_uptime.5.html
  4. Finally we can get the uptime of daemon by getting the difference between step 3 and step 2.
-1
[root@ip-x-x-x-x ec2-user]# ps -p `pidof java` -o etimes=
 266433

pidof java => process id for java process

etimes= => time in Seconds and '=' is to remove header

Rohit
  • 1
  • 1