Posts Tagged ‘cron’Today at work I ran into some issues with cron. One of my Java programs would run fine when executing manually, but would throw a A really nice feature of crontab is that you can set environment variables at the top like this:
and then just have your script use $JAVA_HOME to locate java. This means that the script can be recycled in different environments with different Java versions. Of course, even better would be to not have to wory about $JAVA_HOME at all, and just use the “java” command in the script. This assumes that the java command first found in the path references the correct Java version. In the bash shell this is usually set in the file ~/.bash_profile like this:
However, cron does not read these settings, so they must be set up manually in the crontab file according to above. Which was exactly what had been done in my case:
and this is where it falls apart. This would be nice if it worked, but it does not. Specifically, the leading lines in the cronab syntax are not a script, just a collection of name-value assignments. This means that the expansion of $PATH and $JAVA_HOME fails in the example above. To remedy this you have to repeat the JAVA_HOME variable in the PATH assignment, and also make sure to include everything else of interest, which means you need to do something like this:
Of course, this is all detailed in the manual for crontab. It still took me some time (and help) to fully diagnose the error (mostly due to the fact that I missed that the debug print of the $PATH variable did not expand $JAVA_HOME). Tags: cron, linux |