3

I have scheduled my job to run every day at 12:30 with this command:

30 12 * * * java -jar test.jar

It throws error:

/bin/sh: 1: java: not found

I tried to run this command: java -jar test.jar from shell and it worked just fine.

Now I don't understand. I would say it is happening because JAVA_HOME environment variable is not set, but then why it works from shell?

Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103

4 Answers4

1

The error tells you that the shell could not locate the java binary to execute. This has nothing to do with the JAVA_HOME environment variable, but with the PATH variable which is consulted by sh to find any command.

When you run your task from cron, the shell did not receive the same initialization as your interactive shell where the command works for you. You'll equally find that JAVA_HOME isn't set there, either.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Sounds as a true, what is the resolution then? To add java/bin to the path variable? – Ondrej Tokar Sep 02 '15 at 13:25
  • You should study in general how `cron` is properly used. For example check [here](http://stackoverflow.com/questions/2388087/how-to-get-cron-to-call-in-the-correct-paths). – Marko Topolnik Sep 02 '15 at 13:27
1

Your login shell environment is different from the one your cronjob has. Use envto print your environment.

Check the path on both - Within cron (something like) 30 08 * * * env > ~/cronenv. In your login shell just use env. Then compare the PATH variables. As @Marko Topolnik already stated your PATH within cron obviously does not contain your java executables.

NaN
  • 7,441
  • 6
  • 32
  • 51
0

You can add a line into your crontab file that contains the path that you need:

# m h  dom mon dow   command
PATH=.....

You probably need something like this:

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

You can also use echo $PATH to find out what you have in your regular environment and simply use that value.

Henry
  • 7,721
  • 2
  • 38
  • 38
0

Another option i tried is to use the path of the java installed.

  1. Make sure java is installed with below command

java -version

  1. Check the path of java installed

whereis java

  1. provide the full path to run the app

30 12 * * * /path_from_prev_step/java -jar test.jar

This will also allow you to run this jar with different java portable version if the server has a older version installed.