0

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.

Example.

java -jar MyProgram.jar -- Works fine

MyProg.sh - Works fine

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.

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

Thanks.

A_Elric
  • 3,508
  • 13
  • 52
  • 85
  • This is not really a programming question. It would be more appropriate on superuser.com – Stephen C Sep 05 '12 at 14:20
  • Your diagnostic looks incorrect. If the variables are defined and exported in `MyProg.sh` then they are visible to the `java` process you started, or something in how the script itself is run is severely fishy. Does it have a valid shebang line? – tripleee Sep 05 '12 at 14:23
  • It does indeed have the shabang, I just accidentally omitted it in my copy paste. Our linux sysadmin has been going at this for a little bit and he's been tearing his hair out. – A_Elric Sep 05 '12 at 14:36

3 Answers3

1

JAVA_HOME and PATH doesn't need to be set

Can you try

/usr/data/java/current/java -jar /usr/data/apps/MyProg/MyProg.jar
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Giving this a shot now, I'll let you know if it works when I see the log after the cron runs at 10:40 – A_Elric Sep 05 '12 at 14:38
  • "xception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server." This is what happens when it doesn't find my envir variable. – A_Elric Sep 05 '12 at 14:47
  • Which environment variable do you believe controls that? Are you sure the cron process is being run as the same user? Do you see any errors in the server side logs? – Peter Lawrey Sep 05 '12 at 14:49
  • The exception I posted was from the log the cron generated. The cron is also being run from root I believe. The environmental variable is something like "COMPANY_SQL_SERVER = DEV_SQL_SERVER1" And then it pulls that from a lib included in the program itself. If that variable isn't set I end up with jdbc://null/rest-of-string so it just fails out (which is what I want). However when I simply run the shell script it works out just fine. – A_Elric Sep 05 '12 at 14:53
  • So you do `export COMPANY_SQL_SERVER`? Can you run it from `cron` as the same user it works on the command line as? – Peter Lawrey Sep 05 '12 at 14:57
  • I cannot, no. I just tried adding export COMPANY_SQL_SERVER1 and export COMPANY_SQL_SERVER2 and still came up with the same exception. – A_Elric Sep 05 '12 at 15:36
  • Can you printout your environment variables inside your program to make sure this is the problem? – Peter Lawrey Sep 05 '12 at 15:42
  • I can, yes. They print when run normally, and not when run by cron. – A_Elric Sep 05 '12 at 15:50
  • Running the program with System.out.println("COMPANY_SQLSERVER: " + (NOCvariables.getLiveServer("COMPANY_SQLSERVER", 3306))); Works without issue, when that's called by a cron, it errors out. Env_Var: COMPANY_SQLSERVER1=Dev_Server1.Company.net – A_Elric Sep 05 '12 at 16:00
  • Can you print `System.getenv()` ? – Peter Lawrey Sep 05 '12 at 16:06
  • Doing so now, I'll let you know how it goes. – A_Elric Sep 05 '12 at 16:19
  • 1
    It appears to be running my program as root, which doesn't have any env vars set for it at all! – A_Elric Sep 05 '12 at 16:30
  • Usually cron starts with only the default settings so even if `root` has setting once you login, they might not apply. – Peter Lawrey Sep 05 '12 at 17:28
  • Root doesn't have the env vars I need, I opened a question asking how to add them on superuser, thanks a lot. – A_Elric Sep 05 '12 at 18:02
  • You can add them to the startup script for your JAR. – Peter Lawrey Sep 05 '12 at 19:37
  • I ended up solving this by doing a source /etc/profile.d/MyVars.sh – A_Elric Sep 06 '12 at 14:59
0

I ended up solving this problem by doing a

source /etc/profile.d/MyVars.sh

which got my environmental variables back in place.

A_Elric
  • 3,508
  • 13
  • 52
  • 85
-1

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

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

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 &
Balkrushna Patil
  • 418
  • 6
  • 12
  • This depends entirely on what's in your `.profile`, and often on what other shell startup files you have. Unfortunately, many installers and guidelines are focused exclusively on how to set things up in your interactive shell environment, and then often contain instructions for modifying `.bashrc` and/or `.zshrc` instead of properly configuring the tool for any shell, inculding the default shell used by `cron`. So, this is not wrong, but quite incomplete. – tripleee Jan 01 '22 at 11:39