Operating System - HP-UX
1829873 Members
2253 Online
109993 Solutions
New Discussion

How to determine a particular process on a particular box

 
Boyd Kodama
Frequent Advisor

How to determine a particular process on a particular box

In HP-UX 10.20,

I want a script to kill and restart a process (dtwm, in particular). My script has the following:

kill -9 `ps -deaf | grep -v grep | grep dtwm | grep $user_x | awk '{print $2}'`
dtwm &

where $user_x was the one running the script.
However, when someone tested the script, it failed because that person just happen
to be logged in the same system but at a different terminal.

How can I revise my script to kill the process that is on the box where they are at?

I don't want the process at some other box killed. When I do a "ps -deaf" for the
process the tty is listed as "?". It's the same for the process on the other box.

Any ideas?

thank you,

Boyd
Without Mondays, there weren't be any Fridays
9 REPLIES 9
Stefan Farrelly
Honored Contributor

Re: How to determine a particular process on a particular box


try the who -u command, it matches up a login with the IP address theyre coming from, this should be enough to make your script unique. I thought to restart somebodys window manager (dtwm) you needed to export your DISPLAY variable to their IP anyway ?
Im from Palmerston North, New Zealand, but somehow ended up in London...
CHRIS_ANORUO
Honored Contributor

Re: How to determine a particular process on a particular box

Hi Boyd,

Try using this:
ee=`ps -ef|grep dtwm|grep "?"|cut -c2-8`
kill -9 $ee

When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Rick Garland
Honored Contributor

Re: How to determine a particular process on a particular box

grep for a process PID and not a user PID.

If the process only runs while a particular user is logged in, run in background so the process keeps running after the user logs out.
Andy Monks
Honored Contributor

Re: How to determine a particular process on a particular box

Hi Boyd,

Firstly, I don't advise using kill -9 routinely. It pulls the rug from under the feet of the process. So, any locks/semaphores/shared memory etc don't get released.

So, I'd recommend at least changing the kill -9 to kill -1 or kill -15.

Boyd Kodama
Frequent Advisor

Re: How to determine a particular process on a particular box

Tried the who -u and the so-called IP address came up as ts044d44.lap-ca.concentric.net. I am not sure who to determine which
dtwm would relate to which box give an IP address like this.

By the way, the script is run by the owner of the dtwm. The script would
not be killing somebody else's process, i.e. root will not run this script. I found no need for using $DISPLAY.

Tried the following:
`ps -ef|grep dtwm|grep "?"|cut -c2-8`
but that provides the owner of the process. And since our situation involves a user
with two dtwm processes and we want only one particular one killed, I don't see
how this method will help distinguish the one we want killed.

Here's some output after executing `ps -deaf | grep -v grep | grep dtwm | grep tester`:

tester 20367 20248 0 09:12:55 ? 0:01 dtwm
tester 19219 19143 0 08:33:08 ? 0:07 dtwm

For the idea to "grep for a process PID and not a user PID", I am not sure how grepping for the process PID will help me know which one of these is the right
one to terminate, unless you had in mind a process other than dtwm.

Yes, using `kill -15` is a better choice than using `kill -9`.

Still looking how to pinpoint the dtwm process to terminate.

thanks,

Boyd
Without Mondays, there weren't be any Fridays
CHRIS_ANORUO
Honored Contributor

Re: How to determine a particular process on a particular box

Download and install the lsof program from this link: http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/lsof-4.48/

This program lists all run processes.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Stefan Farrelly
Honored Contributor

Re: How to determine a particular process on a particular box


When a user logs in set a variable in their .profile/.dtprofile which is their DISPLAY (IP address), if this is from an X session then DISPLAY should be set by default, else use it from who -u. Now that session knows its own IP so later if you need to restart their dtwm process you know which one to kill.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Boyd Kodama
Frequent Advisor

Re: How to determine a particular process on a particular box

I was finally able to acheive what I wanted with the following script:

#!/bin/sh
this_pid=$$
orig_pid=$$
command=""

while [ "`basename $command`" != "dtsession" ]
do
ps -ef | awk '( $2 == '"$this_pid"' ) { print $3, $8 }' | read ppid command
orig_pid=$this_pid
this_pid=$ppid
done

kill -s SIGKILL `ps -ef | awk '( $3 == '"$orig_pid"' && $8 == "dtwm" ) { print $2 }'`

dtwm &


thanks,
Without Mondays, there weren't be any Fridays
Rick Beldin
HPE Pro

Re: How to determine a particular process on a particular box

Try using lsof (a public domain tool) to list open files. Grep for port 6000:

# lsof -u rbeldin | grep 6000
dtterm 1869 rbeldin 4u inet 0x40ef0c68 0t1931356 TCP wallace:49534->rbeldin3107.atl.hp.com:6000 (ESTABLISHED)
dtterm 3514 rbeldin 4u inet 0x40ff6868 0t802704 TCP wallace:61128->rbeldin3107.atl.hp.com:6000 (ESTABLISHED)
Xnest 3723 rbeldin 6u inet 0x40d5b668 0t1533916 TCP wallace:61499->rbeldin3107.atl.hp.com:6000 (ESTABLISHED)
Necessary questions: Why? What? How? When?