This question already has answers here:
How to get CRON to call in the correct PATHs
(15 answers)
Closed last year.
So I've run into a bit of an issue. My workplace uses environmental variables on it's machines and we've recently switched our dev / prod servers to unix-based solutions (RHEL 6) and we're trying to get some of our old programs to run with a cron. The envir variables are running on the box itself (Example: Server1=dev-server.intranet.net or something along those lines) but we're running into issues where a cron is in place.
Example.
java -jar MyProgram.jar -- Works fine
MyProg.sh - Works fine
JAVA_HOME=/usr/data/java/current
PATH=$JAVA_HOME/bin
export JAVA_HOME
export PATH
java -jar /usr/data/apps/MyProg/MyProg.jar
When calling MyProg.sh from a cron, it doesn't work, as it can't see the envir variables at all.
Can someone offer some insight to how to make this work with a cron?
Thanks.
JAVA_HOME and PATH doesn't need to be set
Can you try
/usr/data/java/current/java -jar /usr/data/apps/MyProg/MyProg.jar
I ended up solving this problem by doing a
source /etc/profile.d/MyVars.sh
which got my environmental variables back in place.
Cron always runs with a mostly empty environment. HOME, LOGNAME, and SHELL are set; and a very limited PATH.
In order to available all environment variable, We need to load ~/.profile file before running given command.
In my case i used below commands.
40 11 * * * . $HOME/.profile; /shellPath/bin/execute-job.sh START 5 >> /home/sampleuser/cron.log 2>&1
execute-job.sh
nohup java -Dlog4j.configuration=file:/shellPath/conf/log4j.properties -cp /shellPath/libs/scheduler-0.1.jar com.scheduler.Scheduler $1 $2 > /dev/null 2>&1 &
Related
This question already has answers here:
Java command not found on Linux
(6 answers)
Closed 6 years ago.
i try to run spark application on a cluster standalone mode, when i access to some remote machines and use "java -version" command i get the information(java version ..), but on others i get an error
-bash: command not found
so i thought that maybe java is not installed on those nodes so i tried with:
sudo apt -get install openjdk-8-jdk
but i get the same error, so wanna know how can i fix this, and i have some questions:
-Is it necessary that i install java on all remote machines? or if i install it only on the master node it is enough?
-if i have to install it on each node, how can i fix the problem that i explained before? (can not use install command...)
-In my code, i used expressions that are only supported with jdk 8, but some nodes (in which i could get "java version") it is installed jdk7, so do i have to reinstall jdk8 ?
"command not found" error means that particular command you're trying to invoke is not found in neither of directories listed in $PATH system variable.
There are two ways how to fix this:
1) Specify full path when running an executable:
/opt/jdk-12345/bin/java -version
2) add the very same path to the beginning of PATH (change will be applied to current session only):
export PATH=/opt/jdk-12345/bin:$PATH
java -version
To fix this permanently, add that line (export PATH=/opt/jdk-12345/bin:$PATH) to ~/.bashrc (if BASH is default shell for that user) or to ~/.profile
Also because this is Unix Java, make sure to set up LD_LIBRARY_PATH and CLASSPATH variables if you're running some server applications. Usually this is done in application startup scripts, no need to go global.
Please verify which Server OS you're running ( uname -a or /bin/uname -a ) because different Unix systems have different package managers: apt-get is for Ubuntu/Debian, rpm for RedHat, Entropy for Sabayon/Gentoo, etc...
This question already has answers here:
cronjob does not execute a script that works fine standalone
(3 answers)
Closed 6 years ago.
I wrote a bash file in which I am executing java file, its working properly if I am executing it but when I am trying with crontab it is not ,please help.
this is my crontab :
*/5 * * * * /home/import.sh >/dev/null 2>&1
this is my bash file:
- me=$(date +%Y-%m-%d)
mkdir -p /home/importRequirement"$foldername"
{
java -jar ImportRequirement1.jar
java -jar ImportRequirement1.jar
}
2>importRequirement"$foldername"/log$(date +%Y-%m-%d-%H-%M-%S).txt
I have deleted the url.
When a script is intended to be started from a non interactive environment (cron or init), none of the usual goodies such as custom path or other environment variable are set.
The rule is:
ensure all the commands (except at most those in /bin and /usr/bin) use full path
ensure all required environment variables are set
If you use many scripts that way, you could build a setenv script that declares all environment variables and create ones for every command you use. Here it will contain (more or less):
export JAVAHOME=...
export JAVA=/path/to/java
Then you can use in your script:
$(JAVA) -jar ImportRequirement1.jar
but here again you should either have a previous cd to the expected directory or use absolute path of jar
The environment variables for cron jobs is not the same as what users get when they are logged in. Double check the environment variables you need to run your script (ie JAVA_HOME and PATH).
cron might not know with what to interpret your script, you might want to place
#!/bin/bash
on the first line of your bash script.
I have scheduled my job to run every day at 12:30 with this command:
30 12 * * * java -jar test.jar
It throws error:
/bin/sh: 1: java: not found
I tried to run this command: java -jar test.jar from shell and it worked just fine.
Now I don't understand. I would say it is happening because JAVA_HOME environment variable is not set, but then why it works from shell?
The error tells you that the shell could not locate the java binary to execute. This has nothing to do with the JAVA_HOME environment variable, but with the PATH variable which is consulted by sh to find any command.
When you run your task from cron, the shell did not receive the same initialization as your interactive shell where the command works for you. You'll equally find that JAVA_HOME isn't set there, either.
Your login shell environment is different from the one your cronjob has. Use envto print your environment.
Check the path on both -
Within cron (something like)
30 08 * * * env > ~/cronenv.
In your login shell just use env. Then compare the PATH variables.
As #Marko Topolnik already stated your PATH within cron obviously does not contain your java executables.
You can add a line into your crontab file that contains the path that you need:
# m h dom mon dow command
PATH=.....
You probably need something like this:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
You can also use echo $PATH to find out what you have in your regular environment and simply use that value.
Another option i tried is to use the path of the java installed.
Make sure java is installed with below command
java -version
Check the path of java installed
whereis java
provide the full path to run the app
30 12 * * * /path_from_prev_step/java -jar test.jar
This will also allow you to run this jar with different java portable version if the server has a older version installed.
I am unable to run a crontab job , under a different user.(For e.g sudo -u someuser crontab -e)
It runs absolutely fine under my user profile.
I know what is the issue, but cannot find the resolution for it.
The issue is , when i configure this crontab job for other user, crontab is not able to find java ,as a result even simple java -version is not working.
Below is my script.
#!/bin/bash
export JAVA_HOME=/usr/jdk/jdk1.6.0_31
export PATH=/usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/VishalS/bin
echo "JAVA_HOME is: " $JAVA_HOME >> log.out
echo "PATH is: " $PATH >> log.out
which java >> log.out
/usr/bin/java -version >> log.out
/usr/jdk/jdk1.6.0_31/bin/java -version >> log.out
output of above script :
JAVA_HOME is: /usr/jdk/jdk1.6.0_31
PATH is: /usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/VishalS/bin
/usr/bin/java
so obviously, the below lines did not work.
/usr/bin/java -version >> log.out
/usr/jdk/jdk1.6.0_31/bin/java -version >> log.out
Could somebody please help me here? I do not understand why even after setting jdk path crontab does not executes java -version ?
Try setting the paths in the other users crontab directly. See 'man 5 crontab'.
The only that comes to my mind is that, the java command might not have the Executable permission to the user you are trying to execute it from.
So use chmod to give necessary permissions to execute.
Crontab(5) runs without ENV, so you need to source an environment (you are building the JAVA_HOME and PATH, but crontab gives you (almost) nothing. notice that the output of "which java" did not appear in your log file.
build an environment script you can source for your crontab script(s), ". path/to/env.sh"
Put full paths on all programs executed in shell scripts etc. You cannot rely upon environment in cron
run "which which", did you get /usr/bin/which? then put that in your script.
we often omit paths from scripts for convenience, but give paths in scripts run from crontab
does the java run when logged in as the other user?
Thanks everyone for your helpful comments.
However the actual fix which worked in my-case , was mix of steps as mentioned below :-
1. Setup Sun JDK path under root's user profile.(earlier open jdk was setup)
2. Gave permission to logs folder where logs were being written.(earlier the permission were not correctly set)
3. Tweaked my cronjob(i think there was an extra space there)
why the error occurs when i am starting the jboss server 6.0 ?
'findstr' is not recognized as an internal or external command,
operable program or batch file.
It looks like your PATH environment variable hasn't been set up correctly. Does this link help?
http://community.jboss.org/wiki/FindstrCommandNotFound
EDIT: are you sure that the JBoss startup batch script (usually in %JBOSS_HOME%\bin\run.bat) is picking up the PATH correctly? It might be running as a different user with a different PATH. Edit this script and add the line echo %PATH% before the first line that contains findstr. What output does this give you?
The only other place findstr is used (in jboss-6.0.0.20100721-M4 anyway) is in the service.bat script in the same folder as run.bat . Again, you could try putting echo %PATH% before the line in this script that uses findstr if the previous step didn't help you.
EDIT 2: according to your comments, the echo %PATH% line I asked you to add gave the following output:
E:\jdk1.6\bin;E:\apache-ant-1.7.0\bin;E:\jboss-6.0\bin\run.bat
Clearly this doesn't contain C:\WINDOWS\system32, so JBoss definitely won't be able to find findstr. But I don't understand why the PATH is ending up like this. How are you starting JBoss - as a service or by running run.bat? Is JBoss being run under some user account which has been set up with a very restricted PATH? Do you have some other script which is manipulating the PATH before JBoss starts? Also, which version of Windows are you using?
Also, it's not immediately clear to me from your three comments
I'm already checked that the findstr application is already in that path C:\WINDOWS\system32\
E:\jboss-6.0\bin\run.bat
Am also set that in the system variables in Environmental Variables
whether C:\WINDOWS\system32 is in the PATH in Control Panel > System > Environment Variables. Is C:\WINDOWS\system32 in the PATH in the System Variables section within the Environment Variables dialog?
This doesn't seem to be an issue with JBoss. This seems to be more of an issue with the environment within which you are running it. I can quite imagine a lot of other programs would be unhappy with being run in a similar environment.