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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM