Check Process using Shell Script

◇◆丶佛笑我妖孽 提交于 2020-01-25 03:18:28

问题


I am developing a Java program which checks the running processes, if not start that process. In my context I am executing the .sh file which is like this.

#!/bin/sh
echo "Hello World..."
cnt=`ps -eaflc --sort stime | grep clientApplication.jar | grep -v grep | wc -l`
if [ $cnt = 3 ] 
then
        echo "Services for Pigeon are already running..."
else
        echo "Starting Services for Pigeon..."
        echo `java -jar clientApplication.jar`
fi

But it's not working. What's the problem?


回答1:


Using the test expression works. Hope this helps !!

echo "Hello World"
cnt=`ps -eaflc --sort stime | grep clientApplication.jar |grep -v grep | wc -l`
if(test $cnt -eq 3) ;
then
    echo "Services for Pigeon are already running..."
else
    echo "Starting Services for Pigeon..."
    echo `java -jar clientApplication.jar`
fi



回答2:


I am not sure. Try this

cnt=$(`ps -eaflc --sort stime | grep clientApplication.jar | grep -v grep | wc -l`)
if[$cnt -eq 3]; then

Just try this




回答3:


echo won't execute anything it will just print the content what is there in double quotes. So if i have understood your requirement you should be doing this instead of

echo "Hello World" cnt=ps -eaflc --sort stime | grep clientApplication.jar |grep -v grep | wc -l if(test $cnt -eq 3) ; then echo "Services for Pigeon are already running..." else echo "Starting Services for Pigeon..." /bin/java -jar clientApplication.jar fi

Hope this will work, i haven't tested it now


回答4:


  1. Equality checks should be ==, = is assignment
  2. I'd use $(expr) rather than `expr`
  3. if(expr) not if[expr]
  4. not sure if you intend to echo the java line or not, it's not going to start the jar by telling it to print. I got rid of the echo since you said you wanted to start it.

so if you try this, it will work:

    #!/bin/sh
    echo "Hello World..."
    cnt=$(ps -eaflc --sort stime | grep clientApplication.jar | grep -v grep | wc -l)
    if($cnt==3) 
    then
        echo "Services for Pigeon are already running..."
    else
        echo "Starting Services for Pigeon..."
        java -jar clientApplication.jar
    fi


来源:https://stackoverflow.com/questions/14500211/check-process-using-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!