Assuming you are referring to your own user's crontab, to avoid duplicating the definition of JAVA_HOME
you can export
the variable in ~/.zshenv
(instead of ~/.zshrc
), which is read even in non-interactive, non-login shells, and run zsh -c 'sh /path/to/script'
in your cron job (replacing sh
, based on what the program called "Bourne shell" in your question actually is, if appropriate).
Alternatively, if you are fine with defining JAVA_HOME
in multiple places and if your sh
implementation supports this1, you may export
it in ~/.profile
and invoke sh
as a login shell by either appending -l
to the script's shebang or changing the cron job's command into sh -l /path/to/script
.
Though, in the end, the most convenient solution is probably to simply add
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre/
as a line at the top of your crontab (unless you have distinct cron jobs that need distinct values of JAVA_HOME
, of course).
1 Your sh
, which is unlikely to be a "true" Bourne shell, may have a -l
option if it is actually a link to (for instance) bash
or dash
. As Stéphane Chazelas pointed out in a comment, 1) it does not have it if it is the Bourne shell or an implementation of POSIX sh
(e.g., sh
has no -l
option on {Free,Net,Open}BSD); and 2) not all the implementations that support -l
will read ~/.profile
when given that option.
VARIABLE=value
, right? You don't have to set the variable for each and every crontab entry. – Kusalananda Mar 23 '21 at 22:24JAVA_HOME
once in a crontab file. – Kusalananda Mar 24 '21 at 05:03