Search code examples
javalinuxshellrhelbash-trap

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.


Solution

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