环境变量在cron中看不到

时间:2022-09-19 08:06:00

So I've run into a bit of an issue. My workplace uses environmental variables on it's machines and we've recently switched our dev / prod servers to unix-based solutions (RHEL 6) and we're trying to get some of our old programs to run with a cron. The envir variables are running on the box itself (Example: Server1=dev-server.intranet.net or something along those lines) but we're running into issues where a cron is in place.

所以我遇到了一个问题。我的工作场所在其机器上使用环境变量,我们最近将我们的dev / prod服务器切换到基于unix的解决方案(RHEL 6),我们试图让我们的一些旧程序与cron一起运行。 envir变量在盒子本身上运行(例如:Server1 = dev-server.intranet.net或其他类似的东西),但我们遇到了cron到位的问题。

Example.

java -jar MyProgram.jar -- Works fine

MyProg.sh - Works fine

MyProg.sh - 工作正常

JAVA_HOME=/usr/data/java/current
PATH=$JAVA_HOME/bin

export JAVA_HOME
export PATH

java -jar /usr/data/apps/MyProg/MyProg.jar

When calling MyProg.sh from a cron, it doesn't work, as it can't see the envir variables at all.

从cron调用MyProg.sh时,它不起作用,因为它根本看不到envir变量。

Can someone offer some insight to how to make this work with a cron?

有人可以提供一些有关如何使用cron工作的见解吗?

Thanks.

3 个解决方案

#1


1  

JAVA_HOME and PATH doesn't need to be set

不需要设置JAVA_HOME和PATH

Can you try

你能试一下吗

/usr/data/java/current/java -jar /usr/data/apps/MyProg/MyProg.jar

#2


0  

I ended up solving this problem by doing a

我最后通过做一个来解决这个问题

source /etc/profile.d/MyVars.sh

which got my environmental variables back in place.

这使我的环境变量恢复到位。

#3


-1  

Cron always runs with a mostly empty environment. HOME, LOGNAME, and SHELL are set; and a very limited PATH.

Cron总是在一个空无一人的环境中运行。设置HOME,LOGNAME和SHELL;而且PATH非常有限。

In order to available all environment variable, We need to load ~/.profile file before running given command.

为了使所有环境变量可用,我们需要在运行给定命令之前加载〜/ .profile文件。

In my case i used below commands.

在我的情况下,我使用下面的命令。

40 11 * * * . $HOME/.profile; /shellPath/bin/execute-job.sh START 5 >> /home/sampleuser/cron.log 2>&1

execute-job.sh

nohup java -Dlog4j.configuration=file:/shellPath/conf/log4j.properties -cp /shellPath/libs/scheduler-0.1.jar  com.scheduler.Scheduler $1 $2 > /dev/null 2>&1 &

#1


1  

JAVA_HOME and PATH doesn't need to be set

不需要设置JAVA_HOME和PATH

Can you try

你能试一下吗

/usr/data/java/current/java -jar /usr/data/apps/MyProg/MyProg.jar

#2


0  

I ended up solving this problem by doing a

我最后通过做一个来解决这个问题

source /etc/profile.d/MyVars.sh

which got my environmental variables back in place.

这使我的环境变量恢复到位。

#3


-1  

Cron always runs with a mostly empty environment. HOME, LOGNAME, and SHELL are set; and a very limited PATH.

Cron总是在一个空无一人的环境中运行。设置HOME,LOGNAME和SHELL;而且PATH非常有限。

In order to available all environment variable, We need to load ~/.profile file before running given command.

为了使所有环境变量可用,我们需要在运行给定命令之前加载〜/ .profile文件。

In my case i used below commands.

在我的情况下,我使用下面的命令。

40 11 * * * . $HOME/.profile; /shellPath/bin/execute-job.sh START 5 >> /home/sampleuser/cron.log 2>&1

execute-job.sh

nohup java -Dlog4j.configuration=file:/shellPath/conf/log4j.properties -cp /shellPath/libs/scheduler-0.1.jar  com.scheduler.Scheduler $1 $2 > /dev/null 2>&1 &