1834100 Members
2354 Online
110063 Solutions
New Discussion

Re: Argument Checking

 
SOLVED
Go to solution
Chuck Ciesinski
Honored Contributor

Argument Checking

On our HP-UX 11.11i V1 systems, our staff likes to use the -o argument to the ps command when looking for a specific process information. In HP-UX I have to set the UNIX95 variable to do this. Is there another I can use the ps command and get the same results without setting UNIX95?

proc_jobman=$(UNIX95=l ps -u root -o pid,args|grep /opt/maestro/bin/jobman|awk 'BEGIN {ORS = " "} {print $1}')
if [[ -n $proc_jobman ]]
then
#kill the jobman process
echo "sending kill to jobman process $proc_jobman"
kill -9 $proc_jobman
fi

Thanks in advance,

Chuck Ciesinski
"Show me the $$$$$"
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Argument Checking

Well, ps -u root -f (or -x if patched) will also show the argumentrs supplied to the command (-x will show arguments beyond 80 characters). The output is not the same but the data are there. On the other hand, by asserting the XPG4 (UNIX95= ) behavior, the output can be tailored to exactly what you want so not using the XPG4 behavior appears to be a step in the wrong direction. If you hate the syntax so much, you could always define an alias.
If it ain't broke, I can fix that.
Ninad_1
Honored Contributor

Re: Argument Checking

I dont think so there is any other method.
But if its just the pid and args you are interested in then you may also use
ps -u root -f | awk '{print $2,$NF}' ....

Regards,
Ninad
James R. Ferguson
Acclaimed Contributor

Re: Argument Checking

Hi Chuck:

First, release 11.11 is known as 11iv1.

With HP-UX it is necessary to arm the UNIX95 (XPG4) option to be able to use the '-o' switch.

This is in fact the best/safest/easiest way to match a process's basename to 'ps' output and be assured that you find only what you want.

Be setting confining the setting of UNIX95 to the command line for the 'ps' you are keeping the variable set only for the commandline. This is desirable since exporting UNIX95 into your environment may affect other commands in ways you don't want. The 'cp' command is one command that is influenced differently by this setting.

ALSO:

Never kill with 'kill -9' except as a last resort. A 'kill -9' cannot be ignored or trapped and it doesn't give a process any chance to clean up shared memory segments or remove temporary files.

Instead, use a multi-level kill, starting with a hangup; then a simple 'kill -15'; and as a last resort a 'kill -9'.

The following script will kill a process by name in a much safer way:

# cat .killer
#!/usr/bin/sh
myproc=`basename ${1}`
[ -z "${1}" ] && { echo "no process specified!"; exit 1; }
mypid=`UNIX95= ps -C ${myproc} -o pid=`
if [ ! -z "${mypid}" ]; then
kill -1 ${mypid} 2>/dev/null
sleep 3
kill -15 ${mypid} 2>/dev/null
sleep 3
kill -9 ${mypid} 2>/dev/null
fi
exit 0

...Run as ./killer basename

...where 'basename' is the name of the process you want killed.

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: Argument Checking

Per the man page for ps, the -o -H -C options do not exist in ps unless the UNIX95 variable is used. As mentioned, it must always be set temporarily as it affects MANY other commands in unexpected ways. If you would to have all the UNIX95 options available when you type ps, use alias:

alias ps='UNIX95= /usr/bin/ps'

Now you always get the -o (etc) options. As James implied with his scri-pt, get rid of all your ps|grep scripts! grep is a terrible way to locate process names. Just try this to find all the sh processes:

ps -ef|grep sh

If you were to script this and kill the matching processes, you would kill (among other things):

- unhashdaemon and sshd
- all processes owned by josh and sherry
- shells like sh ksh csh bash tcsh

A very bad situation indeed. Always use -C to select a process by it's exact name as in:

UNIX95= ps -fC sh

And another nifty feature: the -o headers for the selected columns can be removed with header= as in:

UNIX95= ps -fC sh -o pid= -o ppid= -o args

By setting all the -o options to a null title, the entire title line disappears, saving an extra test to drop the title line.

Another overlooked feature in ps is the standard (UNIX95 not needed) -u and -p options which find processes by username and PID respectively. So ps is full of 100% accurate selection options. No more grep for ps.


Bill Hassell, sysadmin