Restrict kill commands when running jar file using a shell script Restrict kill commands when running jar file using a shell script shell shell

Restrict kill commands when running jar file using a shell script


I recommend to you use simple start and stop script for your program;

1) create sh script with name start_myprogram.sh and put into the file ;

PR=`basename $0`    cdt=`date +'%H:%M:%S %d/%m/%Y'`cd $HOME/myprogramnohup java -DMY_PROG -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $@cdt=`date +'%H:%M:%S %d/%m/%Y'`

2) create sh script with name stop_myprogram.sh and put into the file ;

#!/usr/bin/shUSER=`whoami`PID=`ps -xfu $USER|  grep java | grep MY_PROG | grep -v grep | awk '{ print $2 }'`if [ -n "$PID" ]thenkill $PIDelseecho MY_PROG is not running.fi

3) start your program ./start_myprogram.sh &

4) anytime whenever you want stop your program ./stop_myprogram.sh

*This is maybe not answer of your question but at least you dont need to implement anything more.


I would suggest the following change in the script to get to the desired requirement.It seems that you need some kind of function which will catch these commands and not let the commands get executed. Shell script can have this kind of functionality by implementing the use of trap.

You can make change in your script like this:

PR=`basename $0`    cdt=`date +'%H:%M:%S %d/%m/%Y'`cd $HOME/myprogram#Add these two lines in the code for catching exit commandstrap '' 20 trap ' ' INTjava -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $@cdt=`date +'%H:%M:%S %d/%m/%Y'`

Its very simple to use traps in shell scripts. Hope this works for you.