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

Solaris / SysV Style ps UNIX
I come from a Solaris background, and my fingers can't seem to get used to typing ps -aux instead of ps -ef. So I wrote this script called ps-ef to save some sanity and format the ps output as close to Solaris style as I could. It will pass on any options to OS X's ps, so something like ps-ef -ww will work too. Just make it executable (chmod +x and put it somewhere in your path.
#!/bin/bash
pformat="user=UID,pid,ppid,cpu=C,start=STIME,tty,time,command"
psarglist=
psarglistlong=
while true ; do
        if [ -z "$1" ]; then
                break;
        fi
        optchar=${1:1}
        case $optchar in
                O|o|p|t|U)
                        shift
                        psarglistlong="$psarglistlong -$optchar $1"
                ;;
                *)
                        psarglist="$psarglist$optchar"
                ;;
        esac
        shift
done
ps -ax$psarglist $psarglistlong -o $pformat
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[8,762 views]  

Solaris / SysV Style ps | 5 comments | Create New Account
Click here to return to the 'Solaris / SysV Style ps' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Solaris / SysV Style ps
Authored by: googoo on Apr 27, '06 10:05:58AM

I feel your pain. It took me FOREVER to remember to type ps -aux instead of ps -ef, but over time it became hard-coded in my brain. Now the problem is that I use ps -aux on Solaris!

-Mark



[ Reply to This | # ]
Solaris / SysV Style ps
Authored by: sjk on Apr 27, '06 12:05:18PM

No Solaris box handy to check right now (it's been in sys-suspend stasis for maybe a year) but there might still be a BSD-style version of ps in /usr/ucb.

I've long had the habit of typing ps options without the '-' prefix, e.g. "ps aux", which works on OS X. But usually I just run a ps wrapper script similar to what cronix posted.



[ Reply to This | # ]
Solaris / SysV Style ps
Authored by: ikioi on Apr 27, '06 08:46:38PM
This is a great hint! I've missed Solaris style ps too. Here's a slight tweak to your script that makes it even more usable. Take the following and save it as ~/bin/ps , and the you can run "ps -ef" with the space between the "ps" and the "-ef". (Don't forget to do a hash -r so that bash will find the new ps in ~/bin/ .)

#!/bin/bash
#echo hi
pformat="user=UID,pid,ppid,cpu=C,start=STIME,tty,time,command"
psarglist=
psarglistlong=
args="`echo \"$*\" | /usr/bin/sed -e 's/-ef$//' -e 's/-ef/-/'`"
set -- $args
#echo "$args"
#set $args
while true ; do
	if [ -z "$1" ]; then
		break;
	fi
	optchar=${1:1}
	case $optchar in
		O|o|p|t|U)
			shift
			psarglistlong="$psarglistlong -$optchar $1"
		;;
		*)
			psarglist="$psarglist$optchar"
		;;
	esac
	shift
done
/bin/ps -ax$psarglist $psarglistlong -o $pformat


[ Reply to This | # ]
missing ptree
Authored by: mzs on Apr 28, '06 02:12:40PM
Your hint reminded me that I missed ptree whenever I was not using Solaris. I use Solaris, FreeBSD, Darwin, and Linux. On Linux I have pstree which is a lot like ptree on Solaris but I use so many different machines that it is not practical for me to compile pstree on all of them. Hence I wrote a little shell script ptree that I use instead. It is usually mounted somewhere and if not I can easily get it onto the new machine:

ptree:


#!/bin/sh

dirname=`/usr/bin/dirname "$0"` || exit

sed=/usr/bin/sed

sys=`/usr/bin/uname -s` || sys=`/bin/uname -s` || exit
case "$sys" in
    SunOS)
	pspre='/bin/ps -o pid,ppid'
	pssuf='-o comm'
	user=-e	# the e option takes precedence over u
	with=1	# don't show sched, init, pageout, and fsflush
	;;
    Linux)	# a lot like Solaris
	pspre='/bin/ps -o pid,ppid'
	pssuf='-o comm'
	user=-e	# the e option takes precedence over u
	with=0
	sed=/bin/sed
	;;
    *)	# Darwin, FreeBSD, etc
	pspre='/bin/ps -o pid,ppid -ax'
	pssuf='-o command'
	user=''
	with=0
esac

case $# in
    0)	# no args, just run with the defaults
	;;
    *)
	case "$1" in
	    -*)	# an argument for ps, pass it on
		;;
	    [0123456789]*)
		# pid to include in the tree
		with="$1"
		shift
		;;
	    *)	# otherwise a username
		case "$sys" in
		    SunOS | Linux)
			uid=`/usr/bin/id | \
			    /usr/bin/awk -F= 'BEGIN { RS=" " }
				/^[Uu][Ii][Dd]=/ { print $2 }' | \
			    "$sed" 's/[^0-9].*//'`
			user="-u $uid"
			;;
		    *)	# Darwin, FreeBSD, etc
			user="-U $1"
			;;
		esac

		shift
		;;
	esac
	;;
esac


$pspre $user "$@" $pssuf | \
    "$dirname"/otree id="PID" pid="PPID" infon=3 with="$with" headn=1
It uses otree to do its work. This is a generally useful awk script for displaying tree-like output. Put otree in the same directory where you put the ptree script.

otree:


#!/usr/bin/awk -f
# headn : if set to 1, then the first record is a header
# id, pid, info : strings in the header matching id, parent id, and info
# idn, pidn, infon : parameter numbers otherwise
# with : an id to include in the tree
NR == headn {
	j=3

	if (idn) j--
	if (pidn) j--
	if (infon) j--

	for (i=1; i <= NF; i++) {
		if (!idn && $i == id) {
			idn=i

			if (j-- == 1) next;
		} else if (!pidn && $i == pid) {
			pidn=i

			if (j-- == 1) next;
		} else if (!infon && $i == info) {
			infon=i

			if (j-- == 1) next;
		}
	}
}

{
	# save parent id
	pida[$idn]=$pidn

	# add to the list children for the parent id
	if (cidan[$pidn]) {
		cidan[$pidn]++
		cida[$pidn]=cida[$pidn] $idn " "
	} else {
		cidan[$pidn]=1
		cida[$pidn]=$idn " "
	}

	# This block finds the beginning of the info string
	j=0
	last=0
	n=length($0)
	for (i=1; i <= n; i++) {
		a=substr($0, i, 1)

		# last is 0 for FS, 1 otherwise
		if (split(a, b) == last)
			continue

		if (last) {
			last=0
			continue
		} else {
			last=1
		}

		if (++j >= infon)
			break
	}

	# tuck away all of the info string
	infoa[$idn]=substr($0, i)

	# tuck away the id in an orderred list starting from 1
	ida[NR]=$idn
}

END {
	# find the root of the tree containing with
	i=with
	while (j=pida[i]) {
		cida[j]=i " "
		i=j
	}

	# There may be no children of i, we need to create a stack
	# thath includes all roots then.
	stack[0]=cida[i]
	if (!stack[0]) {
		stack[0]=""
		for (i=2; i <= NR; i++) {
			j=ida[i]

			j=pida[j]
			if (pida[j])
				continue

			if (seen[j])
				continue

			seen[j]=1
			stack[0]=stack[0] j " "
		}
	}

	# Real awk does not allow us to define functions, so we use
	# arrays of lists as a stack instead of recursive functions.
	j=0
	while (j != -1) {
		i=index(stack[j], " ")
		if (i >= 1) {
			id=substr(stack[j], 1, i - 1)
			stack[j]=substr(stack[j], i + 1)

			for (i=0; i < j; i++)
				printf("  ")

			print id, infoa[id]

			if (cidan[id]) {
				j++
				stack[j]=cida[id]
			}
		} else {
			j--
		}
	}
}
I like my little ptree script so much that I use it even on Solaris. This is mainly because anywhere I can tack on the -o user option and get the username listed as well. Here are some ways to call it:

ptree (all processes)
ptree mzs (all of my processes)
ptree $$ -o user (all processes forward and backward from my shell, also printing the username)
You can tack on whatever options you like, most work fine. For example on Darwin and BSD I use -c or -www often.

[ Reply to This | # ]
Solaris / SysV Style ps
Authored by: rocteur on Apr 29, '06 06:04:09AM

Great hint, as well as the other scripts on this page. I got used to doing

ps jaux

Makes me feel more at home ;-)



[ Reply to This | # ]