检查进程是否正在运行linux shell

时间:2022-03-26 16:24:09

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:

我有一个需要一直运行的java进程。我在cron program中编写了以下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

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

mytest的地方。sh包含必须运行的java类。

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

当我分别执行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)

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

我运行mytest。文件的路径为$JAVA_HOME/bin/java -cp .:/root/lib/* -Djava.rmi.server。代码=文件:/ /文件夹/ -Djava.rmi.server根源。服务器主机名= hostnameip -Djava.security.policy =。政策-Xmx512m -Xms512m mytest.mytestprog

2 个解决方案

#1


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.

当一个作业在cron中执行时,它有一个不同的环境。很有可能,您的.bashrc没有被加载。而且类路径通常是在该文件中设置的。因此,类路径可能是错误的。

For ways to set the environment in cron, see:

要想在cron中设置环境,请参见:

#2


1  

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

我建议使用pgrep,而不是运行这样长的命令列表。

[ $(pgrep -f testjava) ] &&  && echo "running"

#1


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.

当一个作业在cron中执行时,它有一个不同的环境。很有可能,您的.bashrc没有被加载。而且类路径通常是在该文件中设置的。因此,类路径可能是错误的。

For ways to set the environment in cron, see:

要想在cron中设置环境,请参见:

#2


1  

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

我建议使用pgrep,而不是运行这样长的命令列表。

[ $(pgrep -f testjava) ] &&  && echo "running"