2

I am having a java process which needs to be always running. I have written the following shell in cron program to check that java process:

if [ `ps aux | grep testjava | grep -v grep | wc -l` -ne 1 ];then
cd /root/folder
sh mytest.sh >test.log 2>test-err.log &
echo "mytest not running and restarted on "`date` >> /root/check-test.log

where mytest.sh contains the java class which has to be running.

When I execute the shell file separately it executes well. But when I execute the above cron it gives me the following exception :

Exception in thread "main" java.lang.NoClassDefFoundError: mytest/mytestprog
Caused by: java.lang.ClassNotFoundException: mytest.mytestprog
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Where i am going wrong .

Edit:

I run the mytest.sh file with the path as $JAVA_HOME/bin/java -cp .:/root/lib/* -Djava.rmi.server.codebase=file:/root/folder/ -Djava.rmi.server.hostname=hostnameip -Djava.security.policy=server.policy -Xmx512m -Xms512m mytest.mytestprog

ashu
  • 1,339
  • 7
  • 27
  • 43
  • 1
    I suspect that when `mytest.sh` is called, the path is not right; you are not finding the source file. Make sure the necessary environment variables are set explicitly inside your shell script. – Floris Apr 29 '13 at 05:35
  • I am having file to be run in this path /root/folder.And i have given cd /root/folder .,Is there any thing wrong in giving cd command in that cron – ashu Apr 29 '13 at 06:00
  • Does it have the right permissions to run your executables? Cron is running as what user? I would read the links that @spaceknarf has posted - their titles look very promising. – Floris Apr 29 '13 at 06:03
  • Ya it has proper permissions – ashu Apr 29 '13 at 06:17
  • Instead of playing tricks with `ps`, you'd better maintain a `.pid` file under `/var/run`. To write reliable daemons you could try out [http://commons.apache.org/proper/commons-daemon/] and/or [http://commons.apache.org/proper/commons-daemon/jsvc.html]. – Danilo Piazzalunga Apr 29 '13 at 06:54

2 Answers2

1

When a job is executed in cron, it has a different environment. Most likely, your .bashrc is not being loaded. And often the CLASSPATH is set in that file. Thus, the CLASSPATH is probably incorrect.

For ways to set the environment in cron, see:

Community
  • 1
  • 1
Frank Kusters
  • 2,544
  • 2
  • 21
  • 30
  • But in mytest.sh file itself am having the path as $JAVA_HOME/bin/java -cp .:/root/lib/* -Djava.rmi.server.codebase=file:/root/folder/ -Djava.rmi.server.hostname=hostnameip -Djava.security.policy=server.policy -Xmx512m -Xms512m mytest.mytestprog – ashu Apr 29 '13 at 05:58
  • 1
    You could create a test program in Java that prints its class path, and see if it's different inside and outside `cron`. – Frank Kusters Apr 29 '13 at 11:01
1

Rather than running such a long list of piped command I would suggest using pgrep:

[ $(pgrep -f testjava) ] &&  && echo "running"
anubhava
  • 761,203
  • 64
  • 569
  • 643