4

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.

Jolta
  • 2,620
  • 1
  • 29
  • 42
lang_android
  • 171
  • 1
  • 1
  • 10
  • 1
    duplicate: http://stackoverflow.com/questions/1216172/how-can-i-intercept-ctrlc-in-a-cli-application – Kai Iskratsch Jan 13 '16 at 09:27
  • why dont you want write start and stop script ? The question is where do you know which process is should kill when you start your program on background – newuserua_ext Jan 13 '16 at 09:32
  • @Kai Iskratsch: The change has to be accommodated in the script and not in the program. The answer you marked as duplicate copy is using a Java function for adding hooks which is totally irrelevant and in my case it won't even work. Kindly check the question and the answer before marking it as duplicate. – lang_android Jan 14 '16 at 04:48
  • @newuserua_ext: Thanks for answering. Your suggestion of making start stop script will not work for me as its not a background process, its a program which accepts some input from the user and processes it. The sole purpose of restricting certain exit commands is to make sure the user should not be able to get out of the program. – lang_android Jan 14 '16 at 04:52
  • Do you want to trap/catch/restrict these commands(Ctrl + z, Ctrl + c, Ctrl + break) from the shell script which you have written to start the jar file? – V_J Jan 14 '16 at 04:56
  • @V_J: Yes, I want to stop the user from using one of the above commands and exit from the java application which is running on the screen. Say, the program starts and the program is prompting to enter certain input, example your name and in between that the user presses the ctrl+c, ctrl+z to exit from the application. I hope you're understanding the case? Let me know if I am still not clear. – lang_android Jan 14 '16 at 05:00
  • you can also add traps for signals to your shell script, i think the exact syntax is bit dependent on what shell you are using. but when you google shell name and trap you will find examples. but that will only capture the signals if the shell is in foreground, if the jvm is the application that gets keyboard input then you have to handle the signals in java – Kai Iskratsch Jan 14 '16 at 09:17

2 Answers2

3

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.

newuserua_ext
  • 577
  • 6
  • 18
1

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.

V_J
  • 1,081
  • 13
  • 33