Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running jar with cron throws error: '/bin/sh: 1: java: not found'

Tags:

java

linux

cron

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?

like image 492
Ondrej Tokar Avatar asked Sep 07 '25 19:09

Ondrej Tokar


2 Answers

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.

like image 63
Marko Topolnik Avatar answered Sep 09 '25 10:09

Marko Topolnik


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.

like image 33
NaN Avatar answered Sep 09 '25 10:09

NaN