Check if a process is running and execute a program in Linux using shell script

I always run linux applications manually using the terminal during the development stage. This has helped me immensely as I could see all the messages that were printed on to the linux console. I also had the habit of forgetting to start the Java program once the linux machine reboots. Well how many of you have had that experience? Ohh yes, I forgot to mention that I will be using a compiled java program as an example executable file for this tutorial.

Though this is acceptable for a system that is under development, it is not suitable to run an executable program using a terminal in production environment. So I would go about setting up a cron job to check if the process is running and start it up if it is not, using a shell script. This tutorial will give you an understanding of how to check for a running processes and start the process if not running using a shell script in linux.

Please note that shell script is not the only way to check for a running process or to start a process.

The Shell Script:

So let’s name the file testshellscript.sh

You can use any editor like vi to write shell script.

#!/bin/sh

This is the first line of the shell script. It tells the script which interpreter to refer to. Next let us check for the running process

PROCESSFILE=’ExecutableFile.jar’

if ps ax | grep -v grep | grep $PROCESSFILE > /dev/null

I am assigning the jar file name to a variable named PROCESSFILE and then using the linux command ps to check if the process is running.

then

echo “$PROCESSFILE is running, everything is fine”

else

echo “$PROCESSFILE is not running”

#start the process

/usr/local/jdk/bin/java -jar /opt/$PROCESSFILE > /dev/null 2>&1 &

Fi

If there is a running process, we need not do anything to it. So I am simply echoing a message saying that the Java program is running. And if the Java program is not running, I will attempt to start the Java program. The logic of the conditional statement is as follows:

if process running

then

do nothing

else

start the process / java program

end if

As the example executable file is a jar file I am using bin\java to execute the file. This is almost the same command used in a linux terminal. Only difference is the use of full paths. Also all the outputs are directed to the null device which is a special file that discards all data written to it.

Here is the complete code …

#!/bin/sh

PROCESSFILE=’ExecutableFile.jar’

if ps ax | grep -v grep | grep $PROCESSFILE > /dev/null

then

echo “$PROESSFILE is running, everything is fine”

else

echo “$PROCESSFILE is not running”

#start the process

/usr/local/jdk/bin/java -jar /opt/$PROCESSFILE > /dev/null 2>&1 &

Fi

1 comment to Check if a process is running and execute a program in Linux using shell script

  • Grant

    It’s hard to find your posts in google. I found it on 20 spot, you should build quality backlinks , it will help you to rank to google top 10.
    I know how to help you, just type in google –
    k2 seo tricks

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.