I am writing a Built System for Sublime Text 3 for JavaScript unit testing. I am using
js-test-driver as my testing framework. Sublime Text can only execute one command, so I am using a bash script to do the work. js-test-driver is a java application, and I would like to be able to test if it is already running. As far as I can tell, the process name is just java
. Is there any way to figure out what java applications are running inside the java
process? If not, how can I test if a specific java application is running?
2 Answers
You can use the jps
command:
jps | grep "[0-9][0-9]* js-test-driver"
However, it might not be called js-test-driver
, it might be something like js-test-driver.jar
. Try looking at the output of just jps
first.
With the procps-ng
ps
(should be standard on most Linux systems), you can use the -C
option to limit the output to commands of a particular name. Most likely js-test-driver
will be somewhere in the command arguments. You can list all arguments for running java
processes with:
ps -C java -o args=
To test for js-test-driver
anywhere in the argument list, you could do:
ps -C java -o args= | grep -q js-test-driver &&
echo 'js-test-driver running'
You can also use pgrep
, the equivalent to the above would be:
pgrep -f js-test-driver >/dev/null && echo 'js-test-driver running'
Note that if the reason that you are checking if the process is running is to avoid having your script start two at the same time, there are more robust approaches which avoid possible race conditions. See my answer here for an example of a lock file approach.