如何在Ubuntu中为系统用户设置环境变量?

时间:2021-11-18 22:44:14

I have created a system user for my glassfishv3 installation that has no interactive shell, so no environment variables are set on this user when glassfish is started through asadmin.

我为我的glassfishv3安装创建了一个没有交互式shell的系统用户,因此当通过asadmin启动glassfish时,没有为此用户设置环境变量。

I have tried to copy in the environment by using the following commands in my init script:

我试图在我的init脚本中使用以下命令在环境中复制:

jhv=$(grep JAVA_HOME /etc/environment)
$jhv
export JAVA_HOME    
sudo -u glassfish -E $glassfishpath/asadmin start-domain domain1

Inside of asadmin I echo $JAVA_HOME which prints JAVA_HOME correctly.

在asadmin内部我回显$ JAVA_HOME,正确打印JAVA_HOME。

Once glassfish starts up, I launch a web service with a single method that prints the environment key value pairs. JAVA_HOME is not among them. This causes a problem with any script executed by glassfish that requires JAVA_HOME.

一旦glassfish启动,我使用单个方法启动Web服务,该方法打印环境键值对。 JAVA_HOME不在其中。这会导致glassfish执行的任何需要JAVA_HOME的脚本出现问题。

So how do I set JAVA_HOME on a system user/glassfish environment so it persists once the java container is launched?

那么如何在系统用户/ glassfish环境中设置JAVA_HOME,以便在启动java容器后它仍然存在?

1 个解决方案

#1


0  

Normally, the solution woulbe be to add the -E flag to the sudo command. This flag tells sudo to maintain the current user's environment variables when dropping down into root access.

通常,解决方案是将-E标志添加到sudo命令。此标志告诉sudo在进入root访问权限时维护当前用户的环境变量。

It looks like you are already using this flag though so I don't know what the problem is. It could be that the flag is failing silently in your script and you are not being notified. Have you tried running the script's command manual in a shell? What is the result/output?

看起来你已经在使用这个标志,所以我不知道问题是什么。可能是标志在您的脚本中无声地失败并且您没有收到通知。您是否尝试在shell中运行脚本的命令手册?结果/输出是什么?

#1


0  

Normally, the solution woulbe be to add the -E flag to the sudo command. This flag tells sudo to maintain the current user's environment variables when dropping down into root access.

通常,解决方案是将-E标志添加到sudo命令。此标志告诉sudo在进入root访问权限时维护当前用户的环境变量。

It looks like you are already using this flag though so I don't know what the problem is. It could be that the flag is failing silently in your script and you are not being notified. Have you tried running the script's command manual in a shell? What is the result/output?

看起来你已经在使用这个标志,所以我不知道问题是什么。可能是标志在您的脚本中无声地失败并且您没有收到通知。您是否尝试在shell中运行脚本的命令手册?结果/输出是什么?