0

I have a script: giga.sh (shell script BASH) which is running using PUTTY session.

I see most people like to copy using "Control+C" and paste as "Control+V". Few of my wise friends use mouse's right click then copy, then paste and I wondered, don't they know Control+C? They were actually right due to their experience :), like doing that when giga.sh is running for PRODUCTION deployment/task.

What I want is: if giga.sh is running and someone has multiple applications open and where they regularly perform copy/paste operation for taking copy of a text/image from those opened apps, then if Mouse control accidentally comes on the PUTTY window/session, then performing a "Control+C" should NOT, kill a running giga.sh script.

Right now my script TRAPS few signals (2 i.e. INT, 15 i.e. TERM (default) at the moment) within giga.sh.

Does that mean, all I have to do is to remove "2" from the following (see trap...line) and it'll make a running giga.sh script immune to pressing accidental control+c key by a user? I assume it will NOT as we'll not be trapping any sig 2/INT then, wondering if someone has a better idea. New trap / trap_mesg() to handle sig 2 and sig 15 separately, would do it !?

trap_mesg ()
{
 #...do something here..
 #...before actually exiting out...
 #...do something here..
}

#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call="";

trap 'if [ -z ${trap_call} ]; then trap_call="1"; trap_mesg ; fi' 2 15
##################################
AKS
  • 16,482
  • 43
  • 166
  • 258

1 Answers1

0

I assume it will be like this:

trap_mesg_sig2 ()
{
 #...do something here..
 #...Don't exit and warn the user that if he really want to KILL -2/INT
 #...pass some Signal to script PID to continue as normal/resume instead of getting killed
 #   also reset trap_mesg_sig2 varialble back to "".
 #...maintain a counter i.e. if user presses Control+C (kill -2/INT) 3 times, only then 
 #   exit.
 #...do something here..
}

#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call_sig2="";

trap 'if [ -z ${trap_call_sig2} ]; then trap_call_sig2="1"; trap_mesg_sig2 ; fi' 2
##################################



trap_mesg_sig15 ()
{
 #...do something here..
 #...before exiting out...
 #...do something here..
}

#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call_sig15="";

trap 'if [ -z ${trap_call_sig15} ]; then trap_call_sig15="1"; trap_mesg ; fi' 15
##################################
AKS
  • 16,482
  • 43
  • 166
  • 258