Operating System - HP-UX
1820693 Members
2781 Online
109627 Solutions
New Discussion юеВ

CTRL-C trap in shell script

 
SOLVED
Go to solution
Nalin Uduwawala_1
Frequent Advisor

CTRL-C trap in shell script

Gurus out there !!!!!.

Is ther way to trap ctrl-c in a shell script so that nosy, whacky, smart, silly users dont shell out to a place they should not be ?

I mean I dont want them to feel lost in that almost blank green screen!!!!!.

Thanks for any help.
13 REPLIES 13
Andreas Voss
Honored Contributor
Solution

Re: CTRL-C trap in shell script

Hi,

ctrl-c generates a signal 2 (SIGINT) see man signal.
So do this:

trap "echo break!; exit" 2

Regards
federico_3
Honored Contributor

Re: CTRL-C trap in shell script

write :

trap " echo BREAK ; exit " 2


federico
James R. Ferguson
Acclaimed Contributor

Re: CTRL-C trap in shell script

Hi:

...or if you prefer for the user not to see anything, but merely want to disable the Cntl_C interrupt, put this in your script:

trap ''INT

...JRF...
RikTytgat
Honored Contributor

Re: CTRL-C trap in shell script

Hi,

Federico and Andreas are right, but with the 'exit' in the trap command the script will still exit.

So, remove the exit and your users will not be able to interrupt the script using Ctrl-C.

Bye,
Rik.
Devbinder Singh Marway
Valued Contributor

Re: CTRL-C trap in shell script

On other thing is you can view all the signals that you may want to trap , e.g. in your example you wanted to trap ctrl C which is an interrupt if you type kill -l ( thats l for larry not a 1) it will show you all the signals , so you may want to trap more than one .

Seek and you shall find
Nalin Uduwawala_1
Frequent Advisor

Re: CTRL-C trap in shell script

Thank you. I will try them out.

In this shell script I want to give users to exit from the terminal session.Out to the password prompt.

Now when I give 'exit' it does not logout.

is there another command for this ?

I type exit and it logs me out .

But in a shell script it does not react the same way.

Thank you again. It is a nice sunny day out here in Richmond. I hope this is same for all of you.

regards,

nalin.



Ralph Grothe
Honored Contributor

Re: CTRL-C trap in shell script

If you prefer to use Perl, there is a hash called %SIG whose keys are those that the "kill -l" command lists for your system.
If you assign for any of the signal keys a subroutine reference to the %SIG hash you can write your own signal handlers much better than what trap can offer.
See the POD "perldoc perlvar" for examples.
But be cautious, I read in the "Perl Cookbook" that handling system signals through ones own routines can be perilous.
Madness, thy name is system administration
Andreas Voss
Honored Contributor

Re: CTRL-C trap in shell script

Hi,

when putting the script as the login shell in /etc/passwd exit will logout the user.
If you want to logout at shell level you can kill the session process of the user within you script with:

LOGOUT=`who am i -T|awk '{print "kill -9 " $(NF-1)}'`
trap "echo logout;$LOGOUT" 2

Regards
Carlos Fernandez Riera
Honored Contributor

Re: CTRL-C trap in shell script


Put trap in $HOME/.profile.

There is a patch for ksh and trap, is you use ksh.
unsupported
Nalin Uduwawala_1
Frequent Advisor

Re: CTRL-C trap in shell script

Anreas and others ,

Thanks for your input. I have the login sscipt in each person's .profile. Do I use vi to edit the etc/password file ? What will then happen to the stuff in the .profile for eah user ? do I delete it ?
CHRIS_ANORUO
Honored Contributor

Re: CTRL-C trap in shell script

Hi,
Include the following in the users .profile and also the /etc/profile file:
[(trap "" 1 2 3 9 15) and stty intr "^C" or stty intr "".]
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Emiliano Diez
New Member

Re: CTRL-C trap in shell script

As Chris says, remember to trap ALL the signals, since CTRL-PIPE and other combinations might cause the same as CTRL-C, and if you trap only CTRL-C these holes are still open.
Emiliano
Bill Hassell
Honored Contributor

Re: CTRL-C trap in shell script

It's important to understand what the trap signal is really affecting. In the standard /etc/profile, you will see at the beginning:

trap "" 1 2 3

and at the end

trap 1 2 3

This prevents anyone from exiting from the login shell during /etc/profile, a good thing as you can use /etc/profile to deny access to a particular user by exiting to return to the login/password prompt. This trap affects the login shell only.

Just a note about turning traps on/off: In the /etc/profile example, trap "" 1 2 3 says: "Do nothing for signals 1 2 3 (also known as SIGHUP SIGINT SIGQUIT) if they occur during this script's execution."

Now, if you leave off the last line (trap 1 2 3) which reenables the traps to normal behavior, things will seem OK unless your session is aborted with a hangup (disconnect). Now your shell can't respond as it was told to ignore SIGHUP so it runs in a loop forever as there is controlling tty.

In your .profile, you can also disable traps the same way. And if you take an early exit from .profile, you will be returned to the login/password prompt.

However, if you run another script from .profile, you are no longer in your logoin shell, but a subshell so when you exit, you return back to your parent shell.

A solution is to run your script in the parent shell. This is done with 'sourcing' your script using the . (dot) command. Instead of running your script in .profile with just the script's filename, put a dot space in front of it as in:

. /usr/contrib/bin/myscript

This tells the shell to read each line as if it was typed in at the keyboard. Now an exit in myscript will tell the login shell to exit.

All of this assumes standard /usr/bin/sh (the POSIX shell). If your users insist on using strange shells (like csh) then you'll have to become familiar with their login process(like /etc/csh.login) and how to handle traps in those shells.


Bill Hassell, sysadmin