简体   繁体   中英

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?

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:

/opt/Oracle/Java/bin/java

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:

echo $JAVA_HOME

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM