File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes to check and start a process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "to check and start a process" Watch "to check and start a process" New topic

to check and start a process

raj esh

Joined: Apr 03, 2010
Posts: 10
i am new to unix. please give the script
my script need to check a process running or not and if process is not running, script has to start that process.
assuming process as simple java program.
Peter Johnson

Joined: May 14, 2008
Posts: 5852

You can use the ps command to see what is running. To check for Java apps, use:

ps -ef | grep java

Alternately, you could use the jps app that comes with the JDK - it lists the Java processes running and gives the main class name.

AS far as a full script goes, you are on your own,: NotACodeMill

JBoss In Action
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
I would be careful when you "grep" for just for java. It may return other processes that use java.

Also when you execute the command 'grep java'. The actual command 'grep java' will show up in the process status queue.

One suggestion is to grep for the script that will execute the java program. This will only work if the script blocks while the java program program is running.

For example:

This is just one example.
raj esh

Joined: Apr 03, 2010
Posts: 10
thanks a lot.
i understand the script . i will try to do this in bash shell.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17416

An IDE is no substitute for an Intelligent Developer.
anish patil

Joined: May 02, 2010
Posts: 10
use pgrep see man pages for it
while using the ps use -w option ==> ps -aefww | grep tomat
I agree. Here's the link:
subject: to check and start a process
It's not a secret anymore!