Logo Questions Linux Laravel Mysql Ubuntu Git Menu

shell script fails when executed by cronjob, works fine otherwise

I have an executable jar and I have written a shell script to execute it. When I run the shell script manually, it runs fine but when schedule to run it weekly using crontab, it gives the following error -

log_process.sh: line 16: java: command not found

Line 16 in my shell script is -

java -jar $jar_path $logDirectory $logNamePattern $processedLogDirectory $oldResultsDirectory 2>>$log_file 1>&2

Any idea why is it happening that it runs fine when I run it manually but not when it gets run by vrontab job?

like image 693
N.. Avatar asked Mar 25 '23 00:03


1 Answers

Your cron job doesn't have access to the same $PATH variable that you as a user have.

The easiest way to fix this is to open up a terminal, and run this command:

which java

That's going to give you the absolute path of your java executable. For example:


Replace your 'java' command with the whole path.

You might also want to specify the JAVA_HOME variable in your shell script. From your terminal run:


That'll give you another path, like '/opt/Oracle/Java'. In your script (assuming you are using bash), before you run the java command, put:

export JAVA_HOME=/opt/Oracle/Java

Replacing '/opt/Oracle/Java' with the output that the previous echo gave you.

like image 134
chamakits Avatar answered Apr 13 '23 08:04