Unable to execute java program from crontab job of other user - java

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)

Related

Unable to run Jmeter.bat

I'm trying to use Jmeter but am coming across an issue when I try and run it using the Jmeter.bat file.
It's spits out the following error:
'java -version 2>&1 | findstr /i "version"' is not recognized as an internal or external command, operable program or batch file.
Not able to find Java executable or version. Please check your Java installation.
errorlevel=2
Press any key to continue . . .
I've tried googling this and lots of posts mention that this error is usually down to the environment variables not being set correctly, but I'm fairly sure they are as if I type java or javac into the command prompt I get a response.
I've got them set as follows:
JAVA_HOME : C:\Program Files\Java\jdk1.8.0_25
JDK_HOME : %JAVA_HOME%
JRE_HOME : %JAVA_HOME%\jre
CLASSPATH : .;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
PATH : your-unique-entries;%JAVA_HOME%\bin
I tried taking the string it's complaining about and pasted that into the command prompt by itself and it ran fine:
C:\Windows\System32>java -version 2>&1 | findstr /i "version"
java version "1.8.0_25"
So why is it throwing the error when running the batch file but not if I paste it in manually? The batch file is located in a folder in my C drive and I'm running by right clicking and selecting 'Run as Administrator'. If I just double click the .bat file I get the error:
Windows cannot find 'C:\apache-jmeter-2.13_src\bin\jmeter.bat'. Make sure you typed the name correctly, and then try again
Is the above error related or is that a separate permissions issue?
Thanks for any help
From command Prompt go to bin folder of apache jmeter and type following command:
C:\apache-jmeter-3.0\bin> java -jar ApacheJmeter.jar
I also faced similar problem when I extracted the JMeter in C. In my case, i don't have admin rights. I assume it is related to some permission issue. So better copy to D: and then check.
Copy C:\apache-jmeter-2.13_src folder to other drive (D:) or download JMeter and extract in D drive and run the batch file.

Ubuntu now able to set java path

I have installed oracle jdk in /usr/lib/jvm/ and i have setted up path in etc/environment as
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_51
PATH=$PATH:$JAVA_HOME/bin
But still when i am running javac, I am getting following error. The program 'javac' can be found in the following packages:
* default-jdk
* ecj
* gcj-4.6-jdk
* gcj-4.7-jdk
* openjdk-7-jdk
* openjdk-6-jdk
It means javac is not installed or java path has not setted properly, however i am able to see javac,java,jps and other programs in my /usr/lib/jvm/jdk1.7.0_51. I have searched enough about it but still not able to get solution of this problem.
The file /etc/environment is not a file executed by the shell (like a shell script); you cannot use $SOMETHING references in this file. Variables are not substituted in this file. So,
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_51
PATH=$PATH:$JAVA_HOME/bin
the second line will not work like this. You have to put the exact path in.
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_51
PATH=...:/usr/lib/jvm/jdk1.7.0_51/bin
The javac binary (and probably other java binaries) is/are not in your user's $PATH environment variable. There are several ways you can address this:
Add /usr/lib/jvm/jdk1.7.0_51/bin to your user's $PATH
environment variable. You can do this by adding a line similar to
the following in your user's .bash_profile:
export PATH=${PATH}:/usr/lib/jvm/jdk1.7.0_51/bin
You'll have to restart your terminal session for it to take effect.
Create symbolic links to the java binaries from some directory
that's already part of your path (such as /usr/bin)
sudo ln -s /usr/lib/jvm/jdk1.7.0_51/bin/java /usr/bin/
sudo ln-s /usr/lib/jvm/jdk1.7.0_51/bin/javac /usr/bin/
BTW: There are several other java executables in /usr/lib/jvm/jdk1.7.0_51/bin. see the symlink commands for java and javac above. You should run similar command for any other executables you may want to use.
Use the fully qualified path directly on the command line:
$ /usr/lib/jvm/jdk1.7.0_51/bin/javac
https://help.ubuntu.com/community/Java
have you tried this page? Its where I go when I need Java info. You may not have the one you installed set as default.
Could it be that you did not refresh the shell after change in path variable?
if you echo $PATH are the changes present?

Error with crontab running a jar file

I'm running Ubuntu 10.10 with open jdk and crontab. I made a neat little Java application that does some cool stuff and exported it as a runnable jar file. I can run it from the command line just fine, but I needed something that would run the file once every day. So I wrote in a cron job to run a shell script that would actually invoke the jar file. The shell script works fine when run on its own and it looks something like this:
#!/bin/sh
/usr/bin/java -jar /root/proj/CoolStuff.jar
Works perfectly. So I added this to the crontab:
23 14 * * * /root/proj/runScript.sh > /root/proj/log.txt 2>&1
This does not run perfectly. In fact, it doesn't run. log.txt will come out saying "Error". Pretty nondescript, right? I've checked my environment variables, nothing fancy there. Is there anything else I might be missing? Any ideas as to where to go from here? Is there any way I can run this script everyday with ease?
Check the execution permission on that file.
The cron is running with different permissions, not the one you got when logged in.
Also you try to access /root.
Try to relocate your script to another "non-root" directory.
See what the environment for you crontab looks like by commenting out your current /usr/bin/java .. and insert set on a line by its own.
Now from you command line do set > tmpEnvVarList.txt and compare with what you see in log.txt.
It's almost certainly that your .profile or .bash_rc (or other) is setting env vars that are not available to you crontab. You'll need to add . .profile etc to you script.
cbO's ideas are good too.
I hope this helps.

How To Run Java File in Rhel 5 Terminal?

Hi I am new to linux and I'm trying to run java file. I am following this site:
http://www.source-code.biz/snippets/java/7.htm
in "installation instruction" when I reach to,
javac JavaDaemonTest.java
I found error command not found then I go to the place where I install jdk 1.6
usr/java/jdk1.6/bin here I found javac command and then I try to run
javac JavaDaemonTest.java
still I am getting 'command not found error'
my environment variable in bash profile is set that is,
PATH=$PATH:$HOME/bin
export JAVA_HOME=/usr/java/jdk1.6.0_29/
export CLASSPATH=/usr/local/apache-tomcat-5.5.34-src/servletapi/jsr152/examples/WEB-INF/lib/jstl.jar:/usr/local/apache-tomcat-5.5.34-src/servletapi/jsr152/examples/WEB-INF/lib/jstl.jar
export PATH
unset USERNAME
how to get out of it
Thanks in advance.
its nothing about Java here, its just your shell doesn't know where to search for javac
Add to the $PATH env. variable your $JAVA_HOME/bin and try again. It should work now

Do commands run from current directory in a shell script?

In a bash shell script I tried these two versions:
java -jar abc.jar&
and
CMD="java -jar abc.jar&"
$CMD
The first verison works, and the second version complains that abc.jar cannot be found. Why?
Commands do run from current directory in a shell script.
This is why the first command in your test script worked.
The second command may not work because either java isn't in your ${PATH} or abc.jar isn't in your ${CLASSPATH}. You could echo these environment variables or set +x to debug your bash script.
Bash (and others) won't let you do backgrounding (&) within the value of a variable (nor will they let you do redirection that way or pipelines). You should avoid putting commands into variables. See BashFAQ/050 for some additional information.
What is the actual error message you're getting? I bet it's something like "abc.jar& not found" (note the ampersand) because the ampersand is seen as a character in the filename.
Also, the current directory for the script is the directory that it is run from - not the directory in which it resides. You should be explicit about the directory that you want to have your file in.
java -jar /path/to/abc.jar&

Categories