Operating System - HP-UX
1833883 Members
1803 Online
110063 Solutions
New Discussion

Re: How do I ignore Ctrl-C in ksh scripts?

 
SOLVED
Go to solution
Stuart Abramson_2
Honored Contributor

How do I ignore Ctrl-C in ksh scripts?

I showed the DBAs how to run our basic BCV unmount/synch/split/mount scripts.

Today, not for the first time, one of the DBAs "Ctrl-C"-d out of one of the BCV scripts, thus leaving the BCV in an "inconsistent" state - half-way unmounted and halfway still mounted (if you follow my meaning).

How so I modify a ksh script to IGNORE Ctrl-C? I don't want to "trap" it. I just want to ignore it, and keep right on going from where it is.
10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How do I ignore Ctrl-C in ksh scripts?

I assume that Ctrl-C triggers a SIGINT. Confirm this via stty -a and look for the value associated with "intr".

You can now cause a SININT to be ignored via the trap statement using a null string command argument.

trap '' 2

Plan B. Use stty to set intr to an impossible value:
stty intr 0377
but be sure to reset to original value later because a stty command applies to the terminal device rather than a process.
If it ain't broke, I can fix that.
Stuart Abramson_2
Honored Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

Clay:

That looks absolutely right to me. I'll give it a try and post points.

Stuart
Bill Hassell
Honored Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

You probably want to trap all the common interrupts and ignore them. The HP-UX standard /etc/profile does this. Do it this way:

trap "" 1 2 3

or

trap "" HUP INT QUIT

This will do nothing for CTRL-C, as well as a hang-up (connection break, either modem or network) or kill -3 to create a core dump.


Bill Hassell, sysadmin
Stuart Abramson_2
Honored Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

If I do a "stty -a" I get this:

# stty -a

...
intr = ^C; quit = ^\; erase = ^H; kill = ^U
eof = ^D; eol = ^@; eol2 = ^@; swtch = ^@
stop = ^S; start = ^Q; susp = ^Z; dsusp = ^@
werase = ^@; lnext = ^@
...

Does this mean that?:
...SIGINT is ^C
...SIGQUIT is ^\
...SIGKILL is ^U
Mark Grant
Honored Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

Certainly does Stuart. I personally would avoid changing this just to stop people breaking out of an application. It can cause too many headaches should the application fall over sometime or things like that.

I would go with "trap" myself. Have used it many times and it will do the trick for you.
Never preceed any demonstration with anything more predictive than "watch this"
A. Clay Stephenson
Acclaimed Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

Yes, although in stty the '^' (caret) indicates Control so that ^C ---> Control-C.
If it ain't broke, I can fix that.
Stuart Abramson_2
Honored Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

Why don't I see "SIGHUP" and "SIGTERM" in my "stty -a"?
Mark Grant
Honored Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

You can't send those from a keyboard. If you want a list of other things you can't send, try "kill -l" (that a lower case L just to make it clear)
Never preceed any demonstration with anything more predictive than "watch this"
A. Clay Stephenson
Acclaimed Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

Because those signals are not associated with keystrokes. SIGHUP is the 'hangup' signal. Think of "hanging up" the modem -- or powering off the terminal -- there is, of course, no key to press on a terminal if you've done gone and turned the danged rascal off. Similarly, SIGTERM is a system-generated (software) signal most commonly invoked from the shell with a kill -15 PID; there ain't no key associated with it neither.
If it ain't broke, I can fix that.
Stuart Abramson_2
Honored Contributor

Re: How do I ignore Ctrl-C in ksh scripts?

Worked perfectly. Thanks all.

Stuart