0

I am having trouble getting my Java program to run from cron. I am able to recreate the problem, using a simple example, as explained below:

In the file /path/to/javaenv.txt I define my CLASSPATH variable as follows:

export CLASSPATH=\
"/path/to/dir1":\
"/path/to/dir2":\
"/path/to/dirn":\
"/path/to/jar1":\
"/path/to/jar2":\
"/path/to/jarn"

From the command line I am able to execute the Java program quite easily by doing something like this:

source "/path/to/javaenv.txt" && java pkgName.ClassName cmd-line-params > /tmp/test-$(date +%s).txt 2>&1

However, the job does not get executed from cron, eventhough my crontab has the following entry:

* * * * * source "/path/to/javaenv.txt" && java pkgName.ClassName cmd-line-params > /tmp/test-$(date +%s).txt 2>&1
Sandeep
  • 139

1 Answers1

2

There are a few issues in your cron schedule:

  1. The % character has a special meaning in crontabs, and must be escaped as \% if you want to use it as you would ordinary use it on the command line. See How can I execute `date` inside of a crontab job?

  2. The source command may not be supported by the shell that interprets the command in the schedule. This depends on what shell /bin/sh is on your system (dash does not support the non-standard source command). Make sure to use . (a dot) in place of source to make your command portable. See e.g. Can't use `source` from cron?

  3. A possible third issue is whether or not the java executable is found or not, and this depends on the value of the PATH variable in the cron environment. If you want to ensure that java is found, set PATH to include the correct directory in the crontab, the environment file you source, or invoke the executable with its absolute path.

Kusalananda
  • 333,661