简体   繁体   中英

Restrict kill commands when running jar file using a shell script

I have a jar file which is a program which accept user input and processes it. I am running this jar file using the below shell script:

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

The problem I am facing with this is, I want to restrict the user from exiting the application using any of the combinations of the below commands. For example:

Ctrl + z 
Ctrl + c 
Ctrl + break

Please help me.

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/myprogram
nohup 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/sh
USER=`whoami`
PID=`ps -xfu $USER|  grep java | grep MY_PROG | grep -v grep | awk '{ print $2 }'`

if [ -n "$PID" ]
then
kill $PID
else
echo 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 commands
trap '' 20 
trap ' ' INT

java -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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM