Trapping SIGINT after restarting program.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trapping SIGINT after restarting program.
# 1  
Old 09-22-2011
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 this function at anytime by pressing a button, so just running one time won't cut it. I searched Google and the Bash man page on 'exec' but couldn't find anything on this problem. Any advice much appreciated.

Code:
control_c()
{
   echo -e "Would you like to run this function?\n"
   echo -e "Enter y or n: "
   read ans

    if [ "$ans" == "y" ]; then

               # Do function here
               sleep 2
               exec bash test.sh
               exit 0
    else
               exit 0
    fi
}

trap control_c SIGINT


Last edited by Azrael; 09-22-2011 at 10:06 AM.. Reason: Syntax error
# 2  
Old 09-24-2011
Got it. I used exec because it prevented the script from forking. However, I learned that this causes the shell to become interactive. If anyone knows how to do this another way, without forking the process, let me know. For now I'm satisfied.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Programming

Restarting the program

Hi, I use gfortran to run the code. Some times I need to stop the program and restart it. On restarting I need to run the program from the beginning. Is there any script or option available to restart the program from where it stopped? This script/option will be immensely useful for... (2 Replies)
Discussion started by: rpd25
2 Replies

4. 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

5. Shell Programming and Scripting

Trapping program return code

Hello I have a program (prog) that accepts a parameter in order to execute some internal loop grabbing memory in each iteration. I'm using top to monitor the memory usage and to produce an output. Thus I need the program's pid as a parameter to top. I capture pid using myPID=$!. I'm also... (5 Replies)
Discussion started by: pavlosgr
5 Replies

6. 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

7. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: Starcast
5 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. Programming

Cannot catch SIGINT while serial break condition occurs

I setup termios structure with IGNBRK is not set and BRKINT is set. To allow the process to receive signals I call: fcntl(fd, F_SETOWN, getpid()); I have made a signal handler to catch all signals. I can catch SIGINT when pressing ctrl+c but when I send break signal over serial then it cannot... (13 Replies)
Discussion started by: gzz
13 Replies
Login or Register to Ask a Question