11

I'm trying to write a script that will find a particular process based on a keyword, extract the PID, then kill it using the found PID.

The problem I'm having in Solaris is that, because the "ps" results are truncated, the search based on the keyword won't work because the keyword is part of the section (past 80 characters) that is truncated.

I read that you can use "/usr/ucb/ps awwx" to get something more than 80 characters, but as of Solaris 10, this needs to be run from root, and I can't avoid that restriction in my script.

Does anyone have any suggestions for getting that PID? The first 80 characters are too generic to search for (part of a java command).

Thanks.

julian
  • 378
  • 2
  • 7
  • 19
  • 4
    I can run `/usr/ucb/ps awwx` without being root on Solaris 10: (uname -r says "5.10") – Matt Curtis Feb 03 '11 at 23:42
  • I too can run it as non-root (5.10 here). And i observe that ps axww output the already truncated lines (no more than ps -eoargs). So apparently it's not possible to squeeze more from this whatever the approach. –  Feb 04 '11 at 07:51
  • Your assumption is incorrect (see my reply). Please post your script to figure out what is wrong with it. – jlliagre Feb 06 '11 at 07:33
  • 1
    The `ps awwx` command also works on Mac OS X 10.9, as Darwin is Unix too. – Steve HHH May 05 '14 at 17:10

8 Answers8

15

This works for me, at least on Joyent SmartMachine:

/usr/ucb/ps auxwwww
Aaron
  • 829
  • 6
  • 12
10

You assumption about ps behavior is incorrect. Even while you aren't logged as root, "/usr/ucb/ps -ww" doesn't truncate arguments for processes you own, i.e. for processes you can kill which are the only one you are interested in.

$ cat /etc/release
                    Oracle Solaris 10 9/10 s10x_u9wos_14a X86
     Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
                            Assembled 11 August 2010
$ id
uid=1000(jlliagre) gid=1000(jlliagre)
$ /usr/ucb/ps | grep abc
  2035 pts/3    S  0:00 /bin/ksh ./abc aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbb
$ /usr/ucb/ps -ww | grep abc
  2035 pts/3    S  0:00 /bin/ksh ./abc aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccccccccccccccccccccccccccccccccc ddddddddddddddddddddddddddddddddddddddddddd
jlliagre
  • 29,783
  • 6
  • 61
  • 72
  • Note that in solaris 11, /usr/ucb became an "optional component" that you can choose to install. An alternative (only available in solaris 10+) is pargs -l – jrodman Sep 22 '14 at 19:08
3

I would suggest pgrep and pkill - http://www.opensolarisforum.org/man/man1/pkill.html - instead.

Edit 0:

How about this ugly procfs hack instead:

~$ for f in /proc/[0-9]*/cmdline; do if grep -q --binary-files=text KEYWORD $f; \
 > then l=`dirname $f`;p=`basename $l`; echo "killing $p"; kill $p; fi; done

I'm sure there's a shorter incantation for this but my shell-fu is a bit rusty.
Disclaimers: only tested in bash on Linux, would probably match itself too.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
2

pargs will help here. though you'll have to iterate through all of the running procs which is a little annoying. but this will at least show you all of a procs arguments when ps would truncate them.

user@machine:(/home/user)> pargs 23097
23097:  /usr/bin/bash ./test.sh aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbb
argv[0]: /usr/bin/bash
argv[1]: ./test.sh
argv[2]: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
argv[3]: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
argv[4]: ccccccccccccccccccccccccccccccccccccccccc
Josh
  • 784
  • 6
  • 13
  • "pargs" is actually worst as a workaround. When "ps" truncates arguments, i.e. when you haven't sufficient privileges to see all of them (typically you don't own the process), "pargs" refuse to display any of them at all. – jlliagre Apr 27 '11 at 16:00
  • @noisesolo was asking about how to **find and kill** a process so I assume he is looking for a process that he has permission to kill/inspect args. – Josh May 04 '11 at 17:46
  • 1
    Awesome! This is the only solution that worked for me. `ps auxwwww` still only show 80 chars for the argument string – Scott Centoni Oct 03 '14 at 17:14
  • Great! I can make a handy script out of this in no time. – 700 Software Jan 29 '16 at 16:56
2
ps "whatever your options" | cat

Works for me; trying to fool ps that stdout is not a tty.

Kirby
  • 15,127
  • 10
  • 89
  • 104
XYZ
  • 89
  • 5
1

I don't remember exactly about solaris and i don't have an access to it now, only tomorrow, but in any case it's better to order the fields you want — simplifies parsing.

ps -o pid,args

If the output is truncated, maybe setting the column name to long string shall help.

  • This is great advice, but my process name is still too long (I'm looking for a keyword at the end of a long java command). Thanks though! – julian Feb 04 '11 at 15:47
  • As i've mentioned before, probably it's already truncated by system, so it won't be possible altogether. I've checked a manual online about what's printed with -o args, it's stated that "The Solaris implementation limits the string to 80 characters". Probably in /proc it shall be the same. –  Feb 06 '11 at 14:34
  • I was just on a Solaris 11 system with no /usr/ucb/ps so I came to this page. ps -edalf -o pid,args printed out full command lines for me so this solved my problem. So is not always truncated. – Eric Van Bezooijen May 31 '17 at 02:03
1
/usr/ucb/ps -auxww | grep <processname> or <PID>
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
-3

Use the -w option (twice for unlimited width):

$ ps -w -w -A -o pid,cmd 
phihag
  • 278,196
  • 72
  • 453
  • 469
  • 3
    Not with Solaris regular ps: "ps: illegal option -- w", "ps: illegal option -- w", "ps: unknown output format: -o cmd" – jlliagre Sep 10 '11 at 05:55