經過分析/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://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