Operating System - HP-UX
1751926 Members
5044 Online
108783 Solutions
New Discussion юеВ

Re: How to retrieve ksh started x hours ago and kill it.

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

How to retrieve ksh started x hours ago and kill it.

hi

I'd like to find the PID of all process link to a ksh script started 4 hours ago and kill them. Is it possible using a combination of ps, kill, awk commands?

Bests Regards
Den
8 REPLIES 8
Pete Randall
Outstanding Contributor

Re: How to retrieve ksh started x hours ago and kill it.

More information would be needed in order to narrow down which process the orginal ksh script was. Do you know the user or, better yet, the PID? If just the user, you would need to look at all the user's processes (ps -f |grep UID) and make some sort of educated guess as to which process you want to target.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: How to retrieve ksh started x hours ago and kill it.

Hi Den:

I'd use the XPG4 (UNIX95) options of 'ps' to match the *name* of the process exactly and return the elapsed time of the process in the format "DD-HH:MM:SS" which you can then easily dissect.

For example:

# UNIX95= ps -C swapper -opid=-oetime= -ocomm=
0 6-23:52:24 swapper

...would be PID-0 for the 'swapper' having run for 6-days, 23-hours, 52-mintes and 24 seconds.

Note the whitespace after the 'UNIX95='. There is no semicolon before the 'ps' command. This keeps the NIX95 behavior set only for the command duration. To do otherwise may lead to nasty surprises.

The "=' sign after the 'ps' options suppresses the header for that option. This simplifies filtering of the output.

The '-C ' makes 'ps' return only *exact* matches of the process basename.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to retrieve ksh started x hours ago and kill it.

>JRF: # UNIX95= ps -C swapper -opid=-oetime= -ocomm=

A space is missing above and Leo may want to look for ksh:
# UNIX95=EXTENDED_PS ps -C ksh -opid= -oetime= -ocomm=
6101 00:26 ksh
3436 22:21 -ksh

You probably don't want to kill that "-ksh" login shell.
Doug Burton
Respected Contributor

Re: How to retrieve ksh started x hours ago and kill it.

INH
Regular Advisor

Re: How to retrieve ksh started x hours ago and kill it.

Hi Leo,

$export UNIX95=1
$while true; do ps ├в efHx > /tmp/ps.`date -u +%d%h%m%H%M` ; sleep 60 ; done

you will have to kill the processes manually

Regds,
INH
Knowledge is power
Leo The Cat
Regular Advisor

Re: How to retrieve ksh started x hours ago and kill it.

Hello Pete


Yes i know the owner: oracle and I know all scripts potentially in the target of my command. All my scripts are starting by xxx_

Example: xxx_check_instance.ksh

I'm looking for something like:
ps -"option" time-10 -name xxx_*

after that I'll have the PID and I'll be able to kill them....


Bests regards
Den
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to retrieve ksh started x hours ago and kill it.

Hi (again) Den:

> I'm looking for something like:
ps -"option" time-10 -name xxx_*
after that I'll have the PID and I'll be able to kill them....

And that's where my original post is trying to point you. You do:

# UNIX95= ps -C myprocess -opid= -oetime= -ocomm=

...where 'myprocess' is the basename of that which you seek. The PID and elapsed time are returned like this:

1234 1-2:33:44 myprocess

In this example, the PID is 1234 and the elapsed time is one-day, 2-hours, 33-minutes and 44 seconds. It's not difficult to parse-out these values and decide if the process(es) are older than four hours. With the PID, you then kill as necessary.

Regards!

...JRF...

Leo The Cat
Regular Advisor

Re: How to retrieve ksh started x hours ago and kill it.

James answer is the solution for me. Thanks Guys.