18 October 2010

Process Environments

When working on a host, it is sometimes of value to know the environmental
settings, such as PATH, that a process is currently using.  Fortunately,
there a way of obtaining this information for Solaris, Linux, and FreeBSD.
The following is an example for cron from a Solaris x86 box (though sparc
could have been used):

        solaris [0] /usr/bin/ps -ef | /usr/bin/grep cron
            root  1687     1   0   Feb 03 ?           0:00 /usr/sbin/cron
        solaris [0] /usr/bin/pargs -e 1687
        1687:   /usr/sbin/cron
        envp[0]: LOGNAME=root
        envp[1]: LC_COLLATE=en_US.ISO8859-1
        envp[2]: LC_CTYPE=en_US.ISO8859-1
        envp[3]: LC_MESSAGES=C
        envp[4]: LC_MONETARY=en_US.ISO8859-1
        envp[5]: LC_NUMERIC=en_US.ISO8859-1
        envp[6]: LC_TIME=en_US.ISO8859-1
        envp[7]: PATH=/usr/sbin:/usr/bin
        envp[8]: SMF_FMRI=svc:/system/cron:default
        envp[9]: SMF_METHOD=/lib/svc/method/svc-cron
        envp[10]: SMF_RESTARTER=svc:/system/svc/restarter:default
        envp[11]: TZ=US/Eastern

While Solaris has a command to display this information, Linux uses a
file under /proc:

        linux [0] /bin/ps -ef | /bin/grep cron
        root      1770     1  0  2008 ?        00:00:02 crond
        linux [0] /usr/bin/strings /proc/1770/environ
        CONSOLE=/dev/console
        SELINUX_INIT=YES
        TERM=linux
        INIT_VERSION=sysvinit-2.86
        PATH=/sbin:/usr/sbin:/bin:/usr/bin
        runlevel=3
        RUNLEVEL=3
        PWD=/
        LANG=en_US.UTF-8
        previous=N
        PREVLEVEL=N
        SHLVL=3
        HOME=/
        _=/usr/sbin/crond

Finally, FreeBSD can also display this information via ps, though a
little more tersely and not as cleanly:

        freebsd [0] /bin/ps ajwwx | /bin/grep cron
        root    475     1   475   475    0 Is    ??    0:07.77 /usr/sbin/cron -s
        root  28658 95970 28657 95970    2 RL+   p0    0:00.00 grep cron
        freebsd [0] /bin/ps wwep 475 -o command
        COMMAND
        HOME=/ PATH=/usr/bin:/bin /usr/sbin/cron -s

Output length in each case will vary to some extent depending upon
how a process' runtime environment is setup.  For instance, while
the environment output for cron on a FreeBSD box is relatively short
(1 line), as seen above, for a shell spawned upon login via ssh, the
output is 4.5 lines long.