Need to list 3 days old processes

ps -xfu <user name>

this command line will list all the process currently running for <user name>.
I need to filter this output. I need all the process which are running for more than 3 days(excluding demon/sys process) . The list should include PID, PPID, STIME, process/command.

I am using HP-UX machine and I am trying to write a KSH script for this.
Any help will be appreciated.

can you try this ?

ps -eo pid,ppid,command,stime | grep `perl -MPOSIX -le 'print strftime "%b%d",localtime(time-(3*60*60*24))'`

or

UNIX95=1 ps -eo pid,ppid,command,stime | grep `perl -MPOSIX -le 'print strftime "%b%d",localtime(time-(3*60*60*24))'`

Thanks @Ygemici. I tried the command line you provided but returned an error -

ps: command is not a valid field name
Can't locate POSIX.pm in @INC (@INC contains: /opt/perl5/lib/5.00502/PA-RISC1.1
/opt/perl5/lib/5.00502 /opt/perl5/lib/site_perl/5.005/PA-RISC1.1 /opt/perl5/lib/site_perl/5.005 .).
BEGIN failed--compilation aborted.
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
        [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
        -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]

Hmm probably your perl version is not support posix module : `Can't locate POSIX.pm in @INC (@INC contains: /opt/perl5/lib/5.00502/PA-RISC1.1`

it says `Can't locate POSIX` so cant found it

Then you must update the Perl :wink:
https://h20392.www2.hp.com/portal/swdepot/try.do?productNumber=PERL

Dude.... its a client production server. I don't have access to install perl. is it not possible to do it without the help of Perl ?

Also, the command syntax suggested for "ps" is not valid on HP-UX .
Maybe respondant meant?:

UNIX95=1 ps -eo pid,ppid,comm,stime

However the format of the "stime" column is probably not useful for processing in the suggested Perl program because the field format changes from "HH:MM:SS" to "MMM dD" for processes more than 24 hours old.

thanks @methly additional information
perl program's output is month and day of the past times..
maybe could be space in between

"%b %d"

i know like this `comm gives little command` and `command param gives full command however now we can get the output (
and for HH:MM:SS format i add something )

# ps -eo pid,ppid,command,stime | grep -E "$(perl -MPOSIX -le 'print strftime "%b %d",localtime(time-(3*60*60*24))')|$(perl -MPOSIX -le 'print strftime "%b %d",localtime(time-(2*60*60*24))')|[0-9]*:[0-9]*:[0-9]*"

Guys... all these command lines uses perl. I don't have updated perl in my machine. any alternatives.... ?

#!/bin/ksh
x=0
for i in 24 48 72
 do
TZ=GMT+$i date|awk '{print ""$2 " "$3 ; }'|awk {'print $2'}|grep "[^0-9]" > /dev/null 2>&1
    if [ "$?" -eq "0" ]; then
dateone[x]=$(TZ=GMT+$i date|awk '{print ""$2 " "0$3 ; }')
    else
      dateone[x]=$(TZ=GMT+$i date|awk '{print ""$2 " "$3 ; }')
  fi
x=`expr $x + 1 `
done
echo "Listing Process...."
ps -eo pid,ppid,command,stime | grep -v grep | egrep "${dateone[0]}|${dateone[1]}|${dateone[2]}|[0-9]*:[0-9]*:[0-9]*"
 
Listing Process....
  5741  49038 /usr/sbin/sshd2  13:27:55
 48522  49038 /usr/sbin/sshd2  08:43:12
 81056 452821 sftp-server      16:29:56
196110      1 /usr/sbin/esmd     Jul 30
250383  49038 /usr/sbin/sshd2  10:12:40
263736  49038 /usr/sbin/sshd2  09:34:28
396770 479004 /usr/local/bin/g   Jul 31
464121 134209 sh -c /usr/users 17:45:00
464923 134209 sh -c /usr/local   Jul 31
479004 464923 [sh]               Jul 31
491157  49038 /usr/sbin/sshd2  15:20:21
125579   5741 -ksh (ksh)       13:28:00
435433 125579 [ksh]            13:28:00
 67394 406865 [ksh]            10:12:54
406865 250383 -ksh (ksh)       10:12:54
284319 377228 /usr/local/bin/d 09:34:53
364089 523652 bash             09:34:46
377228 364089 menu             09:34:53
107532 306563 ps -eo pid,ppid, 17:47:11
222920 463934 bash             15:20:41
234977 491157 -ksh (ksh)       15:20:27
463934 234977 -ksh (ksh)       15:20:30
467724  43034 bash             17:39:50
..................
..................

Regards
ygemici

@ygemici
I repeat the "-o command" parameter is not valid in HP-UX "ps".

Also the "TZ" trick does not work in HP-UX beyond 24 hours increment. Even then it only works if the local timezone is GMT.
It is also wrong for GMT+24 during BST.

it can work if you set Unix95
i know like this ?

export Unix95=1

I know already TZ trick does not every system if localtimezone is not GMT..Already I said "can he try ?"

Maybe could write a specific script but i have not hp-ux system for now..

However any ideas for intended to solution?

Thanks for your replies guys.... but actually TZ trick is not working on HP-UX ! am thinking...is my requirement that much dificult in HP-UX ?

I think I am wrong ...it will be Jul 15 ...so we need know how to output the exact time

I think I am wrong ...it will be Jul 15 ...so we need know how to output the exact time

In the Find command you have mtine , ctime and atime ... I used the command long back , will try and tel u

No,you can

stat /proc/pid 

then you can find the STIME..Right?
we just need to check /proc/"[0-9]{1,}" folder
Then we can determine which process match...

---------- Post updated at 02:47 AM ---------- Previous update was at 02:40 AM ----------

Oh yes,we can use find them

just /proc/pid folder

---------- Post updated at 03:02 AM ---------- Previous update was at 02:47 AM ----------

for i in `find /proc/"^[0-9]\{1,\}" -maxdepth 1 -mindepth 1 -type d  -mtime +3 2>/dev/null | awk -F"/" '{print $3}' ; do ps -af -p $i ; done

find command to list the old processes...interesting! can u plz brief what exactly this command line is doing ?

for i in `find /proc/"^[0-9]\{1,\}" -maxdepth 1 -mindepth 1 -type d  -mtime +3 2>/dev/null | awk -F"/" '{print $3}' `; do ps -af -p $i ; done

As far as I recall, HP-UX does not have a /proc filesystem.

I don't why I can't run the command now ,maybe my shell changed...It doesn't matter..

So the command is not very good . we use a script.

First we find out the pid of process that has running in the system for more than 3 days.

find /proc -maxdepth 1 -mindepth 1 -type d -mtime +1 2>/dev/null| egrep -v /proc/[a-Z]  | awk -F"/" '{print $3}'  | tee /tmp/a 

Second we can find which process with pid.

while read pid 
do
ps -p $pid -o stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm  # you can modify it to get what you want...
done < /tmp/a

FYI:
[root@unix proc]# ps -p 4062 -o stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
STAT EUID RUID TT TPGID SESS PGRP PPID PID %CPU COMMAND
Ss+ 0 0 tty2 4062 4062 4062 1 4062 0.0 mingetty

---------- Post updated at 09:11 AM ---------- Previous update was at 09:07 AM ----------

Oh no, so we can't use /proc ...

---------- Post updated at 09:28 AM ---------- Previous update was at 09:11 AM ----------

so can we use this option of ps below ?

bsdstart START time the command started. If the process was started less
than 24 hours ago, the output format is " HH:MM", else it
is "mmm dd" (where mmm is the three letters of the month).

[root@unix proc]# ps -ea -o bsdstart,comm,pid | head
START COMMAND PID
18:41 init 1
18:41 migration/0 2
18:41 ksoftirqd/0 3
18:41 watchdog/0 4
18:41 events/0 5
18:41 khelper 6
18:41 kthread 7
18:41 kblockd/0 10
18:41 kacpid 11

So we need to determine the start-time;

HH -gt 70 ?

Then we can say the process is three days old.

An earlier poster asked for ideas which would work with standard HP-UX commands.

We can get the elapsed time accurately from the alternative format of "ps".

For example:

UNIX95= ps -uusername -o etime,comm,pid

(Note the "comm" parameter must not be "command" in HP-UX).

However the "ELAPSED" column is fiddly to process because it is in three possible formats according to how long the elapsed time is.

<space><space><space>MM:SS

HH:MM:SS

D-HH:MM:SS
(Where D=days with no leading zeros)

The hyphen between days and hours is going to be handy because we can immediately eliminate processes which are less than a day old. Depending on the accuracy required for "3 days old" we could just use the days field or use it with the HH:MM:SS fields to create an elapsed time in any units.