I always run Linux programs manually utilizing the terminal through the development stage. It has helped me immensely as I actually could view all the messages that were printed to the Linux console. I also had the tendency of forgetting to begin the Java application after the Linux machine reboots. Well, how many of you've had that experience? Ohh yes, I forgot to mention that I'll be using a compiled java program as an example executable file with this tutorial.
Though that is satisfactory for a system that's under development, it is not fit to run an executable program using a terminal in creation environment. Therefore I would go about setting up a Cron job to test if the process is running and start it up if it is not, employing a shell script. This tutorial will provide you with a comprehension of the best way to check to get running processes and begin the process if not running employing a shell script in Linux.
Please be aware that shell script is just not the single solution to check for a running processor to commence a process. Click here
So let us name the file test shell script.sh
You need to use any editor like vi to write a shell script.
#!/bin/sh
That is the primary line of the shell script. Next, let us assess for the running process
PROCESSFILE='ExecutableFile.jar'
/dev/null|"> grep $PROCESSFILE > /dev/null
I will be putting the jar file name to a variable named PROCESSFILE and then use the Linux command ps to check when the process is running.
Afterward
echo "$PROCESSFILE is working, everything is good."
else
echo "$PROCESSFILE isn't running."
#begin the procedure
/usr/local/jdk/bin/java -jar /opt/$PROCESSFILE ">> /dev/null 2">>&1 &
Fi
If there's a running procedure, we need not do anything to it. So I'm just echoing a message saying that the Java program is running. And when the Java application is not running, I'll try to begin the Java program. The logic of the conditional statement is as follows:
if procedure running
Subsequently
do nothing
else
Begin the process/java software
end if
As the example, the executable file is a jar file I'm using bit java to run the file. This is virtually the same command employed in a Linux terminal. The only difference is the usage of full paths. Additionally, all the outputs are directed to the null device which is a special file that discards all data written to it.
Here is the entire code...
#!/bin/sh
PROCESSFILE='ExecutableFile.jar'
/dev/null|"> grep $PROCESSFILE > /dev/null
Afterward
echo "$PROESSFILE is running, everything is fine."
else
echo "$PROCESSFILE isn't running."
#start the process
/usr/local/jdk/bin/java -jar /opt/$PROCESSFILE ">> /dev/null 2">>&1 &
Fi
25