Operating System - HP-UX
1844311 Members
2942 Online
110230 Solutions
New Discussion

Triming arguments in output from ps -f

 
Leslie Chaim
Regular Advisor

Triming arguments in output from ps -f

Good morning,

Is there a way to eliminate some of the output from ps -f, while leaving others?
Specifically, I would like to eliminate the trailing arguments of a given command.

If this is not possible...
How can I invoke an application requiring arguments, and restrict the arguments from the ps command. I can NOT modify the application to read from an enviroment variable.

Also, the arguments are passed from the command line, thus the <
If life serves you lemons, make lemonade
26 REPLIES 26
Paula J Frazer-Campbell
Honored Contributor

Re: Triming arguments in output from ps -f

Hi
Try using GREP

ie
ps -f|grep "what you want"|grep "what you want inside what you want"

See man grep and man egrep

HTH

Paula
If you can spell SysAdmin then you is one - anon
Rick Garland
Honored Contributor

Re: Triming arguments in output from ps -f

You can use awk to get the info you want from the output.
To invoke commands with options in a script you can use the getopt command.
Tony Constantine_1
Regular Advisor

Re: Triming arguments in output from ps -f

grep works fine but i'm not to sure what you want your output to look like, you could also use awk for example

ps -f|awk '{print $1,$2,$3,$4,$5}'
Dan Hetzel
Honored Contributor

Re: Triming arguments in output from ps -f

Hi,

ps -e
ps -u username

will not show the arguments of the processes

It's difficult to cut the arguments from a 'ps -f' with 'awk' or 'cut' because they
have variable length and position. The number of blank characters isn't fixed either.

What are the 'ps -f' fields that you want to keep ?

Dan


Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Leslie Chaim
Regular Advisor

Re: Triming arguments in output from ps -f

Let me clarify:
I would like to inhibit the output from ps -f. So that other users running a command as
ps -fe | more should not see the arguments passed to my command.

The arguments happen to be a user/password to a secure database.
If life serves you lemons, make lemonade
Rick Garland
Honored Contributor

Re: Triming arguments in output from ps -f

You could create a command alias.

Tony Constantine_1
Regular Advisor

Re: Triming arguments in output from ps -f

you can try creating aliases for specified users

alias ps="ps -fe|grep -v username"
Dan Hetzel
Honored Contributor

Re: Triming arguments in output from ps -f

Hi,

What does your application do if you don't provide the arguments.

For example 'svrmgrl' (oracle) can be called with or without the arguments.
example: svrmgrl system/manager

If you don't provide the arguments, the application will prompt for usermane and password. In this case, an input redirection
(file or here document) can be used.

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Kofi ARTHIABAH
Honored Contributor

Re: Triming arguments in output from ps -f

if your arguments are filenames, perhaps you can use the construct ./././././././././././argument

if you have enough ./././'s you will overflow the number of characters that can be displayed :-)

nothing wrong with me that a few lines of code cannot fix!
Kofi ARTHIABAH
Honored Contributor

Re: Triming arguments in output from ps -f

if your arguments are filenames, perhaps you can use the construct ./././././././././././argument

if you have enough ./././'s you will overflow the number of characters that can be displayed :-)

eg.

#sh ././././././././././././././././././././././././././././././././test&
ps -ef | grep sh

root 22796 22763 0 11:55:46 pts/0 0:00 sh ././././././././././././././././././././././././././././.

does that solve your problem?

Sorry, submit button was acting up.
nothing wrong with me that a few lines of code cannot fix!
Kofi ARTHIABAH
Honored Contributor

Re: Triming arguments in output from ps -f

You could create wrapper script that contains what you would have otherwise had to invoke the command with. then execute this script with the sh and the ./././././././ construct (so that its not possible to determine the argument to the sh program)

nothing wrong with me that a few lines of code cannot fix!
James R. Ferguson
Acclaimed Contributor

Re: Triming arguments in output from ps -f

Hi:

You could create a 'ps' wrapper to replace the real 'ps' command. A crude version of this script would look like this:

#!/usr/bin/sh
if (( `id -u` == 0 )) #...root can see all...
then
/usr/bin/ps.realone "$@"
else
/usr/bin/ps.realone "$@"|awk '{print $1,$2,$3,$4,$5,$6,$7,$8}'
fi

The script needs to be embellished to parse (with getopts, for instance) the arguments passed to it making the trimming done by awk fit the output that will be returned. The script, would be named /usr/bin/ps and the real /usr/bin/ps renamed as suggested.

...JRF...
Leslie Chaim
Regular Advisor

Re: Triming arguments in output from ps -f

I tried the <
I would prefer to pass it in from the command line, and for security measures inhibit the output from ps -f to OTHER users.

If life serves you lemons, make lemonade
Mike Stroyan
Honored Contributor

Re: Triming arguments in output from ps -f

A process can use the undocumented pstat(PSTAT_SETCMD,) call to set the "command" returned by ps for the process. I have attached an example that removes all of its arguments.
There is still a race condition between starting the process and changing the ps output. A ps run during that interval may see the original arguments.
Bill Hassell
Honored Contributor

Re: Triming arguments in output from ps -f

Wow, I am surprised that no one mentioned the 'customize' feature of ps (also known as XPG4 behavior). By defining the UNIX95 variable, you can create *ANY* format for ps. Here is an example where ps is limited to real-userID, ResidentSetSizeKB, PID and the command line args:

UNIX95= ps -e -o ruser,vsz,pid,args

Note: UNIX95= is correct. It means: set the variable UNIX95 to nothing (which defines it) and by putting it on the same line, the variable remains defined only for the ps command. DON'T SET UNIX95 for everyone or even put it into root...there are several behavior changes in both libraries as well as commands that will take place when this XPG4 variable exists. Better to use it as needed.

So for the original question, here is ps -f (equivalent) without any arguments and then followed by the same with just the command (args = command line, comm = command only):

UNIX95= ps -e -o user,pid,ppid,cpu,stime,tty,time

UNIX95= ps -e -o user,pid,ppid,cpu,stime,tty,time,comm

You can arrange and choose the values inn any order (except args must be last when used).


Bill Hassell, sysadmin
Leslie Chaim
Regular Advisor

Re: Triming arguments in output from ps -f

Bill,

Certainly, the UNIX95 variable is a terrific feature, and it will definitely be useful info in the future. But not in this case:)

Again, I would like to run a command in 'some way', so that when OTHERS use the ps -f command THEY should NOT see the args to command.

Hopefuly, this clarifies things.

Thanks,
If life serves you lemons, make lemonade
James R. Ferguson
Acclaimed Contributor

Re: Triming arguments in output from ps -f

Leslie:

I'm curious. What do you find "objectionable" to the solution I proposed (above)?

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Triming arguments in output from ps -f

Leslie:

I'm curious. What do you find "objectionable" to the solution I suggested above?

...JRF...
Maureen Gunkel
Trusted Contributor

Re: Triming arguments in output from ps -f

Leslie:
I've been following this all day, and something in the 'UNIX95' suggestion hit me the right way. If you add an alias to the .profile files for those you DON'T want to be able to see the arguments supplied, and tell them to just use 'ls' rather than 'ls -r', it would work. I just tried it and here is the alias line from my .profile, the output of 'ls', and the output of 'ls -r' in the attached file.
HTH
No matter where you go, there you are.
Maureen Gunkel
Trusted Contributor

Re: Triming arguments in output from ps -f

Oops - I meant to say 'lsr' rather than 'ls'.
And you can tell your users you're trying to help them out!
No matter where you go, there you are.
Maureen Gunkel
Trusted Contributor

Re: Triming arguments in output from ps -f

If I confused you, I'm sorry - I meant 'psf' and 'ps -f', not 'ls' and 'ls -r' - it's been a long day.
No matter where you go, there you are.
Tommy Palo
Trusted Contributor

Re: Triming arguments in output from ps -f

I think Kofi was on the right track.
Let's say that your application is in /usr/local/apps/myapp. Then if you start it like //////////////////usr/local/apps/myapp ARG1 ARG2 ... you would only see slashes in the output from ps -ef. (60 slashes in the beginning should be enough)
Keep it simple
Leslie Chaim
Regular Advisor

Re: Triming arguments in output from ps -f

JRF,

My original problem had noting to do with formatting the output of the ps command. I realize my original question was misleading. I hope that this has been clarified. Your solution deals with formatting the output from the ps command.

Kofi's suggestion does solve the problem as elaborated by Tommy. However, I don't think there is any magic about that:)

By the way, I tried aliasing the command like alias -x run="run user/pass". This did not help; the output from ps -f still displays the user/pass.

For now, I am going with Kofi's solution, unless you can come up with some magic.

Thanks,
If life serves you lemons, make lemonade
Leslie Chaim
Regular Advisor

Re: Triming arguments in output from ps -f

JRF,

One more thing, I am not a real fan of "RedfineTheWorld":)

Thanks
If life serves you lemons, make lemonade