1

I'm writing a cross platform bash script. It needs to use this command #1:

cat /proc/$PID/cmdline

and if the procfs is not available (on OS X for example), it needs to fallback to this command #2:

ps -eo "pid command" | grep "^$PID"

My question is pretty simple: what is the correct way to detect if '/proc' file system exists?

  • For what it's worth, `ps -o command= -p $PID` will give you what you want without the half-anchored _grep_. – pilcrow Sep 21 '14 at 13:35

2 Answers2

4

The "proc filesystem" will almost always be mounted in /proc if present, but this does not have to be the case always (at least in theory). Probably a more portable way is to use mount -t proc to list mounted filesystems of type proc and extract the path from there. If the command returns no paths, procfs is not mounted and you can fall back to the alternative command.

Something along the lines of:

PROC_PATH=$(mount -t proc | egrep -o '/[^ ]+')
if [ "$PROC_PATH" ]; then
    # use procfs
else
    # use alternative
fi

On the other hand, ps should be more portable and always work, so maybe best solutions is just to use ps always?

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
0

Proc filesystem is mounted on /proc.

mount | awk '/proc/ {print $3}'       This will show all mounted proc filesystems with full path
Hackaholic
  • 19,069
  • 5
  • 54
  • 72