0

I have some code

#!/bin/ksh
##########################################################################

$JAVA_HOME/bin/java -jar SocketListener.jar 8182 &    
while[[ ??? ]] do;
sleep 1
done

next_command

Next command must be executed, after loading SocketListener. I have method GetJavaID() {} that can get SocketListener PID. I need something that can compare $(GetJavaID) and pattern[0-9] in while loop. In while loop should be check: Has PID $(GetJavaID) created or not?

2
  • 8
    I can't help noticing that you're asking about bash, but running your script in ksh ... not quite the same thing!
    – ams
    Apr 2, 2012 at 12:31
  • 1
    Anyway, I don't get it? If you already know the PID, why do you have to see if it's running? Either it's running, or else it doesn't have a PID yet, surely?
    – ams
    Apr 2, 2012 at 12:37

4 Answers 4

1

You can say

while ! ps -p $(some method that returns the PID you want) 1>/dev/null ; do
    sleep 1
done

ps with -p pid will return success if a pid matches and failure otherwise.

5
  • You can also validate a process id with kill -n 0 $PID, which avoids external commands.
    – Idelic
    Apr 2, 2012 at 17:45
  • @Idelic: What external commands? Surely you don't mean ps. The requestor knows the PID only by running some command, so I incorporated that fact in to the solution. Replacing the above with while kill -n 0 $(...) ; do is not much different
    – sorpigal
    Apr 2, 2012 at 17:51
  • It is different in that it doesn't run external commands. And I do mean ps.
    – Idelic
    Apr 2, 2012 at 17:58
  • 1
    @Idelic: kill is only a builtin sometimes.
    – sorpigal
    Apr 2, 2012 at 18:58
  • It is a builtin much more often than ps, and it is a builtin for bash, which is tagged in the question.
    – Idelic
    Apr 5, 2012 at 18:01
0

you can get the PID of the "most recently executed background (asynchronous) command" using the "$!"

so this should do the trick:

$JAVA_HOME/bin/java -jar SocketListener.jar 8182 &
JAVAPID=$!
while ps -p ${JAVAPID} > /dev/null
do
  sleep 1
done
echo "done"

i redirect the output of "ps" to /dev/null to avoid unneccessary output.

apart from that i fail to understand why you need to background one process only to wait for it afterwards. the following should be equivalent:

$JAVA_HOME/bin/java -jar SocketListener.jar 8182
echo "done"

unless, of course, you want to do something more between starting you process and printing "done".

0

Greg's wiki to the rescue. In Process Management:

kill -0 $PID will check to see whether a signal is deliverable (i.e., the process still exists). If you need to check on a single child process asynchronously, that's the most portable solution. You might also be able to use the wait shell command to block until the child (or children) terminate -- it depends on what your program has to do.

And

There is no such thing as "the daemon started up successfully", and if your specific daemon were to have a relevant definition to that statement, it would be so completely daemon-specific, that there is no generic way for us to tell you how to check for that condition.

I don't understand why you accepted the solution you did. First, if you want to wait for the process to terminates, there's no need to background it. And while ! ps -p $PID is going to run until the process terminates (just like the much simpler wait would do), so it all boils down to a single line:

$JAVA_HOME/bin/java -jar SocketListener.jar 8182
-1

use PS command to get list of all running process's PID and grep for the PID. The number of output must be greater than one to ensure that PID is running( Grepping the PID will be one process listed in the output so we are checking for more than one output). Use wc command to get the number of output. So the command will be

 result=`ps axu | grep -c $(GerJavaID)`  #-c will make it to return number of output lines
 if [ $result -gt 1 ]
 then
   echo "Process Running"
 fi
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.