Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Java Command Not Running in Cron

Tags:

java

shell

sh

cron

We are trying unsuccessfully to run a java command in the following shell script:

#!/bin/bash
/usr/java/jdk1.8.0_91/bin/java -jar /home/cyberren/cyberren/cyberexe/D50_1_Migration.jar > d50Migration.log

We call it with the following line in our cron file:

45 08 * * * /home/cyberren/queries/d50migration.sh

We have confirmed that the shell script runs via cron, but it doesn't execute the java. It tries, but our log output is just this one line:

2016-Jun-30 08:58:11 - D50_1_Migration Process v104

We expect many more lines following such as this:

2016-Jun-30 08:58:11 - D50_1_Migration Process v104
2016-Jun-30 08:58:11 - migrateData -> SQL: UPDATE D50_1 SET xassessment = 192, iStepsTotal = 9 WHERE xIndx = 128
2016-Jun-30 08:58:11 - migrateData -> SQL: UPDATE D50_1 SET xassessment = 192, iStepsTotal = 9 WHERE xIndx = 129
...

We are able to run the same shell script manually, and it executes the java. We are also able to execute the java line manually, and it runs correctly.

So to repeat:

  • The cron job runs as scheduled
  • The shell script runs
  • The log is created, but only with the initial line
  • The java line in the shell script does not run
  • Both the shell script and the standalone java line run manually successfully
  • We have confirmed all directories referenced

Thanks in advance for the thoughts and advice.

Updates/Additional Info:

  • (Edit) Permissions are correct
  • (Edit) Tried adding a preceding cd "$(dirname "$0")" line per Alex's advice. Same output.

Solution:

  • @Jackson, you probably don't have access to the source code for the jar file, so debugging would be fruitless. Try setting up the cron job so that it is called from a bash login and sends STDERR output to STDOUT and logs both in a file, this should do it, 45 08 * * * /bin/bash -l -c "cd /home/cyberren/queries; ./d50migration.sh 2&>1 |tee -a /home/cyberren/d50migration.log" this way you should get to see all output and error output generated (if any) – Finbarr O'Brien 22 mins ago
  • At first we were pretty happy to have a way to look at our errors, but your cron line successfully ran the .jar! You've solved our problem and given us a way to successufully schedule and run java archive files via cron.
like image 248
Jackson Avatar asked Mar 05 '26 19:03

Jackson


1 Answers

Try setting up the cron job so that it is called from a bash login and sends STDERR output to STDOUT and logs both in a file, this should do it:

45 08 * * * /bin/bash -l -c "cd /home/cyberren/queries; ./d50migration.sh 2>&1 |tee -a /home/cyberren/d50migration.log" 

this way you should get to see all output and error output generated (if any)

like image 52
Finbarr O'B Avatar answered Mar 07 '26 09:03

Finbarr O'B