问题
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
回答4:
- Equality checks should be ==, = is assignment
- I'd use $(expr) rather than `expr`
- if(expr) not if[expr]
- 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