为所有由systemd管理的服务新增全局env环境变量的方法
2023-5-4 08:30:0 Author: dyrnq.com(查看原文) 阅读量:6 收藏

/etc/profile或者/etc/security/limit.d这些文件中配置的环境变量仅对通过pam登录的用户生效,而systemd是不读这些配置的,所以这就造成登录到终端时查看环境变量和手动启动应用都一切正常,但是systemd无法正常启动应用 https://blog.51cto.com/u_14036245/4309759

global env

要为所有由systemd管理的服务设置全局环境变量,可以按照以下步骤操作:

创建一个名为/etc/systemd/system.conf.d/env.conf的文件,如果system.conf.d目录不存在,则需要创建该目录。可以使用以下命令创建该文件和目录:

mkdir -p /etc/systemd/system.conf.d/
cat >/etc/systemd/system.conf.d/env.conf<<EOF
[Manager]
Environment=MY_VARIABLE=value
EOF

其中MY_VARIABLE是要设置的环境变量的名称,value是其值。您可以将这些替换为自己的环境变量和值。

执行以下命令重载systemd配置:

systemctl daemon-reload
systemctl show-environment

现在,所有由systemd托管的服务都将包含指定的环境变量。您可以通过执行systemctl show-environment命令来验证是否已经设置了全局环境变量。

global limit

cat /etc/systemd/system.conf

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See systemd-system.conf(5) for details.

[Manager]
#LogLevel=info
#LogTarget=journal-or-kmsg
#LogColor=yes
#LogLocation=no
#LogTime=no
#DumpCore=yes
#ShowStatus=yes
#CrashChangeVT=no
#CrashShell=no
#CrashReboot=no
#CtrlAltDelBurstAction=reboot-force
#CPUAffinity=1 2
#NUMAPolicy=default
#NUMAMask=
#RuntimeWatchdogSec=0
#RebootWatchdogSec=10min
#ShutdownWatchdogSec=10min
#KExecWatchdogSec=0
#WatchdogDevice=
#CapabilityBoundingSet=
#NoNewPrivileges=no
#SystemCallArchitectures=
#TimerSlackNSec=
#StatusUnitFormat=description
#DefaultTimerAccuracySec=1min
#DefaultStandardOutput=journal
#DefaultStandardError=inherit
#DefaultTimeoutStartSec=90s
#DefaultTimeoutStopSec=90s
#DefaultTimeoutAbortSec=
#DefaultRestartSec=100ms
#DefaultStartLimitIntervalSec=10s
#DefaultStartLimitBurst=5
#DefaultEnvironment=
#DefaultCPUAccounting=no
#DefaultIOAccounting=no
#DefaultIPAccounting=no
#DefaultBlockIOAccounting=no
#DefaultMemoryAccounting=yes
#DefaultTasksAccounting=yes
#DefaultTasksMax=15%
#DefaultLimitCPU=
#DefaultLimitFSIZE=
#DefaultLimitDATA=
#DefaultLimitSTACK=
#DefaultLimitCORE=
#DefaultLimitRSS=
#DefaultLimitNOFILE=1024:524288
#DefaultLimitAS=
#DefaultLimitNPROC=
#DefaultLimitMEMLOCK=
#DefaultLimitLOCKS=
#DefaultLimitSIGPENDING=
#DefaultLimitMSGQUEUE=
#DefaultLimitNICE=
#DefaultLimitRTPRIO=
#DefaultLimitRTTIME=
cat >/etc/systemd/system.conf.d/limits.conf<<EOF
[Manager]
DefaultTimeoutStopSec=20s
DefaultLimitCORE=infinity
DefaultLimitNOFILE=infinity
DefaultLimitNPROC=infinity
DefaultTasksMax=infinity
DefaultLimitMEMLOCK=infinity
EOF
systemctl daemon-reload
systemctl show properties --no-pager | grep Limit

local env and limit

[Unit]
Description=hadoop datanode service
After= network.target
[Service]
Type=forking
User=hduser
Group=hadoop
Environment = JAVA_HOME=/usr/lib/jvm/temurin8
Environment = JRE_HOME=/usr/lib/jvm/temurin8/jre
Environment = CLASSPATH='.:/usr/lib/jvm/temurin8/jre/lib/rt.jar:/usr/lib/jvm/temurin8/lib/dt.jar:$JAVA_HOME/lib/tools.jar'
Environment = HADOOP_HOME=/opt/hadoop
Environment = HADOOP_CONF_DIR=/opt/hadoop/etc/hadoop
Environment = HADOOP_LOG_DIR=/data/hadoop/logs
Environment = HADOOP_PID_DIR=/data/hadoop/pid

ExecStart=/bin/sh -c '/opt/hadoop/sbin/hadoop-daemon.sh start datanode'
ExecStop=/bin/sh -c '/opt/hadoop/sbin/hadoop-daemon.sh stop datanode'

Restart=always
TimeoutStartSec=30
TimeoutStopSec=20
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
LimitNOFILE=512000
LimitNPROC=512000

[Install]
WantedBy=multi-user.target

文章来源: https://dyrnq.com/systemd-env/
如有侵权请联系:admin#unsafe.sh