检查进程是否正在运行linux shell无法正常工作

时间:2013-04-29 05:32:24

标签: java linux shell

我有一个需要一直运行的java进程。我在cron程序中编写了以下shell来检查java进程:

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

其中mytest.sh包含必须运行的java类。

当我单独执行shell文件时,它执行得很好。但是当我执行上面的cron时,它给了我以下异常:

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)

我哪里出错了。

编辑:

我运行mytest.sh文件,路径为$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

2 个答案:

答案 0 :(得分:1)

cron中执行作业时,它具有不同的环境。最有可能的是,您的.bashrc未加载。通常在该文件中设置CLASSPATH。因此,CLASSPATH可能不正确。

有关在cron中设置环境的方法,请参阅:

答案 1 :(得分:1)

我建议使用pgrep:

,而不是运行这么长的管道命令列表
[ $(pgrep -f testjava) ] &&  && echo "running"
相关问题