Linux获取进程的环境变量

在linux操作系统中,每个进程都有自己的环境变量,我们可以通过一下方法获取进程的环境变量。
1,获得进程的PID
如:获取oninit进程的PID

ps -ef | grep oninit

获取到oninit的进程号有下列

gbasedbt  8492     1  0 May17 ?        00:36:19 oninit -vy
root      8495  8492  0 May17 ?        00:01:23 oninit -vy
root      8496  8495  0 May17 ?        00:00:19 oninit -vy
root      8499  8495  0 May17 ?        00:00:14 oninit -vy
root      8501  8495  0 May17 ?        00:00:20 oninit -vy
root      8503  8495  0 May17 ?        00:00:00 oninit -vy
root      8531  8495  0 May17 ?        00:00:14 oninit -vy
root      8535  8495  0 May17 ?        00:03:14 oninit -vy
...

2,通过/proc/PID(上一步获取的PID)/environ,读取环境变量

cat /proc/8492/environ |tr '\0' '\n'

输出示例:

XDG_SESSION_ID=32542
HOSTNAME=h01.gbasedbt.com
SHELL=/bin/bash
TERM=xterm
HISTSIZE=10000
IFX_NO_TIMELIMIT_WARNING=y
GBASEDBTSQLHOSTS=/opt/gbase/etc/sqlhosts
OLDPWD=/home/gbase/temp/20240517/insert
CSDK_HOME=/opt/gbase
CLIENT_LOCALE=zh_CN.utf8
USER=gbasedbt
...

标签: PID, 进程环境变量, environ

添加新评论

Free Web Hosting