What can you do to stop the parent script running in this situation? You could unplug your computer, hold the power button down for 10 seconds to turn it off, or throw it into the chip-pan fire to stop it working, but these solutions seem insensible due to the possible loss of data which may occur.
So basically you run a script that makes everything inaccessible, and you want quick and easy solution to stop it . An alternative to powercycling is to hold SYSRQ key (it's same as PRTSC key in many cases), and press one by one R, E, I,S,U,B. Basically that's a safe way to kill all running processes and restart the system.
You need to kill the whole process group. The process group ID (PGID) is the PID of the process that started the group. To kill the process group you need to find the PID of the process that started the process group i.e. the script or command that started it all. The syntax is:
Usually you can use top, htop or similar to find the process and and kill it (or if you know the name or PID of the process, you can killall, pkill etc). For example, using top, I can select a script running from terminal and kill it by pressing k+Enter:
If a user hits Control-C when a script is running, they get an Interrupt> prompt, where they have to type exit or quit to return to the shell. I'd like to have the interpreter exit immediately when Control-C is hit. To this end, I'd like to catch the interrupt and run Exit. Is there a way to do this? Or is there a better option?
Is there anyway in a PERL script to do this:I have an infinite loop statement but I want the script to not beterminated if someone does a control C ^C. Is there actually a way totell PERL to ignore a ^C and continue with the loop. I tried telling it yeah, and I tried the slashes the quotes and everythingbut it still doesn't want to recognize the ^CI tried it many ways but the below is just an example:if ($input = '^C') keep running loop[download]Comment on Control C in PERL?
JeffR-R-R--R-R-R--R-R-R--R-R-R--R-R-R--L-L--L-L--L-L--L-L--L-L--L-L--L-L--[reply][d/l][select]Re: (jeffa) Re: Control C in PERL?by brassmon_k (Sexton) on Apr 21, 2001 at 01:35 UTCCool the reason I wanted to know that is because I'm going to play a little trick on my buddyHe thought it was funny to alter my autoexec.bat on my PC at work so it would go into a booting loopYou know the old trick...Type autoexec.bat and the bottom of the autoexec.bat and it will go into a boot loopAnyway I'm getting him back. I'm going to do it on his UNIX machine (Note this is just a user terminal not somethingimportant because I'm not a jerk) Anyway It's going to be kind of like the infamous "cookie" script except it will makehim guess something he can never guess. It will keep asking for different things and the answer will never relate to the question being asked and I needed the CTRL-C deal so I could stop him from terminating the script. I already took care of itif he tries to open up another shell and terminate the shell process. Or I could just do this one....I like this one...Make an alias for the cd command that does a kill -9 on the openwindows processso whenver he does a cd (It takes a few tries to figure out what's killing open windows)you get booted out of openwindows. It's really fun to watch. Hey were having fun and being inventive at the same time. The BrassMonk[reply]Re: Control C in PERL?by jwest (Friar) on Apr 18, 2001 at 01:43 UTCA control-C is actually an interrupt signal, by the timeyour terminal driver is done mangling it. Install a signalhandler for INT that ignores the signal. Such as: $SIGINT = 'IGNORE';[download]Hope this helps!--jwest->
In my script I use the Appskey hotkey to send Control C. The problem is: when there is nothing to copy, Windows 7 makes an annoying sound. I haven't found a solution that can be implemented in the script, for anyone that uses my script.
I know how to disable system sounds manually in my computer, but that it not the solution I need. The script may be used in different computers. Besides, I don't want to disable the sounds completely, only when I hit Control C and there is nothing selected.
Bash Bits are small examples and tips for Bash Scripts. This bash bit shows youhow to capture a Control C signal in a bash script, for example, to clean up anytemp or pid files when your script is killed or closed.
Exit signals are sent when for example you use pkill or killall. If you donot specify a number, a SIGTERM is sent. If you for example do a pkill -9firefox, it sents a SIGKILL. If you have a bash script which places a tempfile, or a pid file, you might want to clean that up before you exit.
We mentioned earlier that programs in general can be set up to "trap"specific signals and process them in their own way.The trapbuilt-in command lets you do this from within a shell script.trap is most important for "bullet-proofing" large shell programsso that they react appropriately to abnormal events -- just as programsin any language should guard against invalid input. It's alsoimportant for certain systems programming tasks, as we'll see in thenext chapter.
Of course, cmd can be a script orfunction. The sigs can be specified by name or by number. You can also invoke trap without arguments, in which casethe shell prints a list of any traps that have been set, usingsymbolic names for the signals.If you use trap -p, the shell prints the trap settings ina way that can be saved and reread later by a different invocation of the shell.
This just pauses for 60 seconds (the sleep(1) command)and repeats indefinitely. true is a "do-nothing" command whose exitstatus is always 0. For efficiency, it is built-in to the shell.(The false command is a similar "do-nothing"command whose exit status is always 1. It is also built-in to the shell.)As it happens, sleep is also built-in to the shell.Try typing in this script. Invoke it,let it run for a little while, then type CTRL-C(assuming that is your interrupt key). It should stop,and you should get your shell prompt back.
Invoke the script again. Now hit CTRL-C. The odds are overwhelmingthat you are interrupting the sleep command (as opposed totrue). You should see the message"You hit control-C!", and the script will not stop running;instead, the sleep command will abort, andit will loop around and start another sleep.Hit CTRL-\ to get it to stop.Type rm core to get rid of the resulting core dump file.
The relationship between traps and shell functions is straightforward,but it has certain nuances that are worth discussing. The mostimportant thing to understand is that Korn shell functions(those created using the function keyword;see Chapter 4) have their ownlocal traps; these aren't known outside of the function.Old-style POSIX functions (those created using thename() syntax)share traps with the parent script.
This time the looping code is within a function, and the trapis set in the surrounding script. If you hit your interrupt key,it prints the message and then prints "exiting..."It does not repeat the loop as above.
This sets up the command trap trapcode SIG to runright after the function exits, at which time the surroundingshell script is in scope (i.e., is "in charge"). When that commandruns, trapcode is set up to handle the SIG signal.
When the script receives an INT or TERM signal, it savesthe temp file and then exits. Note that the command stringisn't evaluated until it needs to be run, so $msgfilewill contain the correct value; that's why we surround the stringin single quotes.
But what if the script receives a signal before msgfile iscreated -- unlikely though that may be? Then mv will try torename a file that doesn't exist. To fix this, we need totest for the existence of thefile $msgfile before trying to save it. The code for this isa bit unwieldy to put in a single command string, so we'll use afunction instead:
At this point you may be thinking that one could get seriouslycarried away with signal handling in a shell script.It is true that industrial strength programs devote considerableamounts of code to dealing with signals. But these programs are almostalways large enough so that the signal-handling code is a tiny fractionof the whole thing. For example, you can bet that the real Unixmail system is pretty darn bullet-proof.
However, you will probably never write a shell script thatis complex enough, and that needs to be robust enough, to merit lotsof signal handling. You may write a prototype for a programas large as mail in shell code, but prototypes by definitiondo not need to be bullet-proofed.
Therefore, you shouldn't worry aboutputting signal-handling code in every 20-line shell script you write.Our advice is to determine if there are any situations in which a signalcould cause your program to do something seriously bad and addcode to deal with those contingencies. What is "seriously bad"?Well, with respect to the above examples, we'd say that the casewhere HUP causes your job to terminate on logout is seriously bad,while the temporary file situation in our mail program is not.
But when I type control-c, the script just stops and the message is not displayed. I checked trap -l and saw that control-c is intr, but the same thing happened whet I tried that. Can anyone tell me what I'm missing?
Thanks etresoft, but I really wanted to know what was wrong with the example in my book -- I'm new to UNIX and I'm not really clear on the example you gave me. All I'd like to know at the moment is why I can't see the message displayed when I hit control-c when I run the script. Could you help me out there?
I've copied the script from the book -- the format in my post is what happened when I copied it from another site. I too have the separate lines -- but for the life of me it won't display the message -- it just goes back to the loop,which it's supposed to do, but it won't display the message. 041b061a72