Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Is there a better way ?' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Is there a better way ?
Authored by: barrysharp on Jan 20, '02 10:08:38AM

Well I believe you can eliminate the 'head -1' and save having to launch the 'head' command. I'm assuming the 'head -1' is simply there to pick off the first line if multiple Apps were found by 'grep'.

/bin/ps -axww | /usr/bin/grep '[/]APPNAME'| awk '{print $1;exit}'

This will accomplish the same without using the trailing 'head -1'.

Also, since you're using absolute path name for 'grep' then why not for the other commands such as /bin/ps and /usr/bin/awk.

Thus...

/bin/ps -axww | /usr/bin/grep '[/]APPNAME'| /usr/bin/awk '{print $1;exit}'

would be the finished product.

For what it's worth... Remember that /bin/ps isn't guaranteed to always return ALL the process information ALL the time. The /bin/ps command makes the best attempt to produce an accurate process report. The kernel proc table is typically in a state of flux and is maintained by the kernel as a linked list. This aspect will at times mean that when /bin/ps requests process information from the kernel the kernel cannot always return a 'clean' proc table to /bin/ps. I've seen at times over the years hanging /bin/ps processes because they've been delivered a corrupt or incomplete proc table that has circular link pointers.

Regards... Barry Sharp



[ Reply to This | # ]
Is there a better way ?
Authored by: barrysharp on Jan 20, '02 10:36:42AM

...also it's always good practice to sanity check a value such as PID to see if it conforms. In this case the PID must be numeric. A /bin/sh function for doing this is as follows.

function isnumber
{
# Return TRUE status = 0 if argument contains ONLY digits
[[ $1 = +([0-9]) ]]
}

Thus one could script the following to sanity check the PID value

if ! isnumber `/bin/ps -axww | /usr/bin/grep '[/]APPNAME'| /usr/bin/awk '{print $1;exit}'`; then
echo PID is not numeric
else
echo PID is numeric
fi

This technique might be useful to make the "Nicer" feature more robust and maybe to avoid it doing stupid things. For example let's say that PID for some strange reason were to be the string "`/bin/rm *`". Now executing /usr/bin/renice $PID would be rather nasty. ;-)

Sanity check anything and everything that is done -- especially if it's done under the root account.

Regards... Barry Sharp



[ Reply to This | # ]