Intercepting SIGINT in a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Intercepting SIGINT in a bash script
# 1  
Old 07-01-2009
Intercepting SIGINT in a bash script

I've written a bash script which captures video with DVgrab. Because of the nature of the tapes that I am digitizing, sometimes I want to quit capturing before the time that I set for DVgrab. When this is the case I press Ctrl-c and DVgrab exits cleanly, my problem is that there is additional information that the script prints in the terminal with 'echo' when the script runs till it's set time. When I Ctrl-c none of this information prints...

Is there a way to intercept the SIGINT ( I think this is what I want, but I could be wrong) and have it operate exactly as it does by default, but additionally echo a few varibles in the terminal?

Preferably I would like to do this just in my script... and not effect the way Ctrl-c functions system wide.

Not sure if it matters, but based on my reading prior to this post I wasn't sure, but I run this script in a virtual terminal in gnome on an Ubuntu (8.10) Linux machine

The bit that I wanted to add when Ctrl-c is pressed looks like this:

Code:
echo -e "\E[31;40m++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "+               Warning - Capture Ended Early                           +"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "\E[30;47m++ Format: $FORMAT    ++ Min Remaing on $destination : $minremain    "
echo "++ Deck: $deck_number          ++ Customer: $customer      " 
echo "++ Rec Time: $tape_length  ++ Tape: $tapename"

Thanks in advance!
-Starcast
# 2  
Old 07-01-2009
Code:
#!/bin/bash
trap 'echo "Control-C disabled." ' 2
... your code here.

# 3  
Old 07-01-2009
I ended up making it look like this, But you gave what I needed to get there, thanks.

Code:
#!/bin/bash
trap 'echo -e "\E[31;40m\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n
+                   WARNING - Early Exit                         +\n
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\E[30;47m\n
++ Format: $FORMAT    ++ Min Remaing on $destination : $minremain    \n
++ Deck: $deck_number          ++ Customer: $customer      \n
++ Rec Time: $tape_length  ++ Tape: $tapename"' 0

-Starcast
# 4  
Old 07-01-2009
Code:
trap "some command goes here" 0

executes on shell script exit
# 5  
Old 07-01-2009
Yes, I see that.... I just finished a 2 hr tape and my warning poped up after it ran for the full allotted time and exited normally...

I changed the -2- to -0- just guessing and i thought it was working.... ( I can't for the life of me find a listing of 'trap options' explaining what each number here would do?) I switched it because the -2- option isn't ending the script? It prints my inserted code, but doesn't bring back the prompt, it just continues the script?

---------- Post updated at 04:50 PM ---------- Previous update was at 04:27 PM ----------

Ok, I spoke too soon. the -2- option does stop DVgrab (it doesn't exit the script when i press Ctrl-c before the point in the script when DVgrab starts.... Which is fine except, I also have a function that runs a countdown in my script, and this countdown continues after Ctrl-c... The countdown function looks like this:

Code:
function countdown
{
        local OLD_IFS="${IFS}"
        IFS=":"
        local ARR=( $1 )
        local SECONDS=$((  (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2]  ))
        local START=$(date +%s)
        local END=$((START + SECONDS))
        local CUR=$START

        while [[ $CUR -lt $END ]]
        do
                CUR=$(date +%s)
                LEFT=$((END-CUR))

                printf "\r%02d:%02d:%02d" \
                        $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

                sleep 1
        done
        IFS="${OLD_IFS}"
        echo "        "
}

Is there something that I could include in my -trap- code that would kill this function and print my warning?

Thanks
-Starcast
# 6  
Old 07-06-2009
Ok, so I turned my brain on and figured out my solution... I hate it when I catch myself getting lazy on here...

For anyone following the thread, this does what I described needing
Code:
trap 'CUR=$END 
sleep 1 
echo -e "\E[31;40m\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\n+                   WARNING - Early Exit                         +
\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\E[30;47m\n++ Format: $FORMAT    ++ Min Remaing on $destination : $minremain    
\n++ Deck: $deck_number          ++ Customer: $customer      
\n++ Rec Time: $tape_length  ++ Tape: $tapename"
exit ' 2

Oh, I added the 'sleep' to account for DVgrab's exit info that it prints... otherwise I was getting a few lines printed after the warning...

And I added 'exit' at the end so that the script didn't continue after killing the countdown function

-Starcast
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Always pass SIGINT in ksh

The following command will run and wait for input from the user. /usr/sap/SAP/webdisp/wdispmon pf=/usr/sap/SAP/webdisp/profile What I would like to do is (in one command): - Add the above line to a ksh script - Receive the output - and send a SIGINT I have seen many posts on how to... (3 Replies)
Discussion started by: sapsid
3 Replies

3. UNIX for Dummies Questions & Answers

SIGINT issue

May i know what are the possible causes for SIGINT other than ctrl-c? Thanks (17 Replies)
Discussion started by: pandeesh
17 Replies

4. Shell Programming and Scripting

Trapping SIGINT after restarting program.

Tried to add a function to my control_c interrupt here. It works but has one little bug. If the user selects to run the function instead of exiting, the program restarts itself without forking as it should. However, after that control_c no longer works again. I wanted to allow the user to run... (1 Reply)
Discussion started by: Azrael
1 Replies

5. Shell Programming and Scripting

Sh script sends SIGINT to a process

Hello, What I want to accomplish is this: I have made a very simple script that runs 2 commands, polipo proxy and tor. The script runs successfully and the output of tor is visible to the screen. I am using the trap command in order to catch the ctrl+c button combo in order to stop polipo... (5 Replies)
Discussion started by: redsolja
5 Replies

6. Shell Programming and Scripting

Strange SIGINT propagation between Parent/Child sh scripts

Good day, I am trying to add signal handling capabilities to some of my scripts. Unfortunately, I am having some difficulty with the manner in which signals are propagated between parent/child processes. Consider the following example: I have the following "parent" script: #!/usr/bin/sh... (5 Replies)
Discussion started by: danie.ludick
5 Replies

7. Programming

why multiple SIGINT raises when i hit C-c

hi, in my application, i have set up to capture SIGINT and execute a handler.the problem is whenever i hit C-c, multiple SIGINT are sent to the application.I have blocked the SIGINT right after catching the first one but it is unsuccessful.Here is what i do : jmp_buf main_loop; int... (1 Reply)
Discussion started by: Sedighzadeh
1 Replies

8. Programming

[C] fgets problem with SIGINT singlal!!!

Hi all, I have this method to read a string from a STDIN: void readLine(char* inputBuffer){ fgets (inputBuffer, MAX_LINE, stdin); fflush(stdin); /* remove '\n' char from string */ if(strlen(inputBuffer) != 0) inputBuffer = '\0'; } All work fine but if i... (1 Reply)
Discussion started by: hurricane86
1 Replies

9. Programming

Problem with handling SIGINT

For a program I am designing, which involves handling the keyboard input Ctrl^c (SIGINT), it is taking ages for the program to actually recognise and perform the corresponding action whenever I run it and hit Ctrl^C at the CL. I have to do at least 3 Ctrl^Cs before the program will actually... (3 Replies)
Discussion started by: JamesGoh
3 Replies

10. AIX

AIX intercepting system calls

hi, How to intercept existing system calls by kernel extensions on AIX? (2 Replies)
Discussion started by: ramkumar.pvs
2 Replies
Login or Register to Ask a Question