Operating System - HP-UX
1833758 Members
2575 Online
110063 Solutions
New Discussion

How to get process creation date using script?

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

How to get process creation date using script?

How do I get the process creation date/time for a process? I want to be able to get the info via a script.

I've got a process that's been running quite a while and I want to run a script that will print the process creation date and time.

jack...
14 REPLIES 14
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to get process creation date using script?

Hi Jack:

The 'stime' from 'ps' represents the process start time. Once it exceeds 24-hours, only the date is given.

For instance, you could report the uid, command, and starting time for all processes with:

# UNIX95= ps -ef -o uid,comm,stime

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: How to get process creation date using script?

UNIX95= ps -p ${pid} -o stime

If it ain't broke, I can fix that.
Jean-Louis Phelix
Honored Contributor

Re: How to get process creation date using script?

Hello,

I'd rather give you this example derived from 'man pstat_getproc'. It will be much more precise ...

Regards,

Jean-Louis.

#include
#include

main (argc, argv)
int argc;
char *argv[];
{
struct pst_status pst;
int target = atoi(argv[1]);

if (pstat_getproc(&pst, sizeof(pst), (size_t)0, target) != -1)
{
(void)printf("Process %d started at %s", target, ctime(&pst.pst_start));
}
else
{
perror("pstat_getproc");
}
}
It works for me (© Bill McNAMARA ...)
Jack C. Mahaffey
Super Advisor

Re: How to get process creation date using script?

Thanks to all for such a quick response. This web site rules!!!

jack
Jack C. Mahaffey
Super Advisor

Re: How to get process creation date using script?

My version of ps does not support the -o option. Here's my attempts:
[8238]root@freprod:/var/adm/sw/save # ps -ef | grep 6323
frs2mgr 6323 6322 226 10:59:33 ? 15:15 MxCase -d maint
root 8186 16113 2 11:20:07 pts/0 0:00 grep 6323
[8239]root@freprod:/var/adm/sw/save # ps -p 6323 -o stime
ps: illegal option -- o
usage: ps [-edaflP] [-u ulist] [-g glist] [-p plist] [-t tlist] [-R prmgroup]
[8246]root@freprod:/var/adm/sw/save #


Jean-Louis... I'm an idiot when trying to write c code. I wouldn't know how to compile. Thanks anyway... :)

A. Clay Stephenson
Acclaimed Contributor

Re: How to get process creation date using script?

That's what the UNIX95= ps does. It puts ps in XPG4 mode.
If it ain't broke, I can fix that.
Paula J Frazer-Campbell
Honored Contributor

Re: How to get process creation date using script?

Jack

You need unix95 for the -o option:-

AS Per Janmes post.


UNIX95= ps -ef -o uid,comm,stime



Paula
If you can spell SysAdmin then you is one - anon
Justo Exposito
Esteemed Contributor

Re: How to get process creation date using script?

Hi Jack,

Try this:
ps -ef | grep 6323 | sed 's/\ \{1,\}/#/g' | cut -d"#" -f6

Regards,

Justo.
Help is a Beatiful word
James R. Ferguson
Acclaimed Contributor

Re: How to get process creation date using script?

Hi (again) Jack:

The format is:

# UNIX95= ps ...

Note carefully that the variable UNIX95 is set only for the duration of the command line. It is written as "UNIX95= ", that is with a space character *before* the 'ps' command.

This arms the UNIX95 (XPG4) behavior and thereby allows use of the '-o' option list to create a custom 'ps' output.

Regards!

...JRF...

Jack C. Mahaffey
Super Advisor

Re: How to get process creation date using script?

Thanks all. Just learned something new about using UNIX95=

Justo, thanks... Awsome... I added grep -v to exclude the grep invocation.

All examples worked. Thanks again.
jack...
Jean-Louis Phelix
Honored Contributor

Re: How to get process creation date using script?

Hello Jack,

Juste save it in prog.c and use 'make prog'. That's all ... Call the program with a pid number.

Regards,

Jean-Louis.
It works for me (© Bill McNAMARA ...)
Jack C. Mahaffey
Super Advisor

Re: How to get process creation date using script?

Jean-Louis, Thanks... I now know how to compile. This should be a good week... :)
Justo Exposito
Esteemed Contributor

Re: How to get process creation date using script?

You are welcome Jack.

Regards,

Justo.
Help is a Beatiful word
Jack C. Mahaffey
Super Advisor

Re: How to get process creation date using script?

Jean-Louis - Your example was what I needed. It includes the full timestamp. Thanks...
jack...