CentOS 7下使用chkconfig添加的服务无法使用/etc/profile里面的环境变量

经过分析/etc/profile为入口的,基本是登录后执行的变量,而使用chkconfig添加的服务多变以守护经常运行,没有登录。

CentOS 7下使用chkconfig添加的服务无法使用/etc/profile里面的环境变量,通过env输出只有:

TERM=xterm
SYSTEMCTL_SKIP_REDIRECT=
PATH=/sbin:/usr/sbin:/bin:/usr/bin
PWD=/
SHLVL=1
SYSTEMCTL_IGNORE_DEPENDENCIES=
_=/bin/env

所以要想在chkconfig添加的服务里使用环境变量,只有在服务的脚本中引入变量文件,比如:source /etc/profile,但不建议引入这个文件,直接想要哪个变量引入这里面的个别脚本:/etc/profile.d/xxx.sh

但使用systemctl添加的服务,可以通过这个变量设置:

EnvironmentFile

参考:

https://stackoverflow.com/questions/42499166/environmental-variables-not-being-picked-up-in-linux-service

https://forums.fedoraforum.org/showthread.php?244785-environment-variables-in-chkconfig

https://unix.stackexchange.com/questions/44370/how-to-make-unix-service-see-environment-variables

https://coreos.com/os/docs/latest/using-environment-variables-in-systemd-units.html

猜你喜欢

转载自www.cnblogs.com/EasonJim/p/9950656.html