0

I'm trying to get the information on a PID via c or terminal (ideally I would like to get it both ways, multiple methods)

I have a PID and would like to figure out the time it was claimed. By claimed i mean when a program started using it. Or if a PID was reused, when the latest program that is using it, started to use it.

In Linux what I do is lstat "/proc/PID_HERE/exe" or lstat "/proc/PID_HERE/cmdline" but I cant figure out how to do this on Mac OS.

Note: I changed from stat to lstat because a single exe is being used with command line arguments to open multiple instances. So each instance gets a new pid, so I want info on that specific instance, thus on Linux I have to use lstat. So any lstat equivalent to get pid info on mac os?

halfer
  • 19,824
  • 17
  • 99
  • 186
Noitidart
  • 35,443
  • 37
  • 154
  • 323

1 Answers1

2

I think you mean this:

ps -p <PID> -o start=
10:22am

where you substitute in your PID. The start= selects the start time and also suppresses the header line. If you want the header, use

ps -p <PID> -o start
STARTED
10:22am

Alternatively, you can get the start time formated more fully like this:

ps -p <PID> -o lstart=
Fri 26 Sep 10:22:50 2014

By the way, if you want a list of the keywords (like start and lstart above) you can either wade through the manage, or more simply, just give an invalid keyword and it will tell you all the ones it likes :-)

ps -o %rubbish
ps: %rubbish: keyword not found
ps: no valid keywords; valid keywords:
%cpu %mem acflag acflg args blocked caught comm command cpu cputime etime f flags gid group ignored
inblk inblock jobc ktrace ktracep lim login logname lstart majflt minflt msgrcv msgsnd ni nice nivcsw
nsignals nsigs nswap nvcsw nwchan oublk oublock p_ru paddr pagein pcpu pending pgid pid pmem ppid pri
pstime putime re rgid rgroup rss ruid ruser sess sig sigmask sl start stat state stime svgid svuid
tdev time tpgid tsess tsiz tt tty ucomm uid upr user usrpri utime vsize vsz wchan wq wqb wql wqr xstat
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Wow so so nice thank you!!! Is there any `c` functions that can do this in mac os? Like linux we have `lstat`. The `c` functions run much faster than `bash` stuff because we have to run it then read it. – Noitidart Sep 26 '14 at 10:47
  • Oh also I don't understand what you mean by the "By the way" stuff can you please explain that a little more please. – Noitidart Sep 26 '14 at 10:48
  • 2
    To get this information within a program, see here http://stackoverflow.com/questions/220323/determine-process-info-programmatically-in-darwin-osx – Mark Setchell Sep 26 '14 at 11:03
  • 1
    The `by the way` stuff was just mean tfor anyone else reading my answer in case they happened to want the cputime or something else other than the start time, it was just a hint for people wanting a slight variation of what you asked for. – Mark Setchell Sep 26 '14 at 11:04
  • Thanks man so much agian. So I was thinking ps must use some `c` functions to get that info no? What c functions would be responsible for `lstart=` and `start=`? Is there like source code for this somewhere? :) Ill check that topic thanks man! I think that question answers this question haha thanks bro! – Noitidart Sep 26 '14 at 11:07