Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: bash control-c trap

  1. #1

    Default bash control-c trap

    My script code is very complex. So, I'll just show the general format, leaving out most of the code. If you pressed control-c, it would cancel the scan and unmount the drive. This no longer works. I'v done many searches online. The code should work. It just exits the script without unmounting it.

    scanvirus_trap_flag=false
    # run if user hits control-c
    control_c()
    {
    scanvirus_trap_flag=true
    #adding unmount drive command doesn't do anything.
    }

    # trap keyboard interrupt (control-c)
    trap control_c 2

    #loop mount drives

    #mount drive

    clamscan -r ${mountpoint}

    #unmount drive

    if [ "$scanvirus_trap_flag" = 'true' ]; then
    break
    fi

    #end loop

  2. #2

    Default Re: bash control-c trap

    Have you tried defining he traps BEFORE anything else, in particular
    before the functions?


    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below...

  3. #3

    Default Re: bash control-c trap

    Quote Originally Posted by ab View Post
    Have you tried defining he traps BEFORE anything else, in particular
    before the functions?
    Tried that didn't work. One time, control-c exited the script. Another time adding control-z trap, the script wouldn't exit. I'm narrowing the script to a simple script that shows the problem.

    control-c = 2, control-z = 20


    Code:
    #!/bin/bash
    trap_key=false
    
    # capture the interrupt control-c
    trap 'trap_key=controlc' 2
    
    clamscan -r /
    
    if [ trap_key = 'controlc' ]; then
        printf "clamscan interrupted\n"
    else
        printf "clamscan completed
    fi
    This format did work on my current script. If the user pressed control-c, the drive would be unmounted and the script would exit. In this case, Control-c interrupts clamscan. Then the script detect control-c and prints the message.

  4. #4

    Default Re: bash control-c trap

    FYI, this is a page with similar problem.

    http://stackoverflow.com/questions/1...ing-trap-ctrlc

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,384

    Default Re: bash control-c trap

    If I understand you correct, you have a script where you trap the Crtl-C interrupt. Inside the script you run a program. When the program is running, you do a Crtl-C. That interrupts the program (not the script). The program seems to exit as the result of the interrupt. End of story.

    You seem to think that the fact that the program is interrupted is carried forward (or better: backward) to the parent process (the script), that is of course not the case. The script can only act on the return code of the exiting pogram. It depends on the exiting program if it creates a return code that signals a Crtl-C interrupt that differs from other return codes. The documentation of the program should tell you what return codes it creates in what situations.
    Henk van Velden

  6. #6
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,475
    Blog Entries
    2

    Default Re: bash control-c trap

    Just a FYI,

    If you're looking for an example trapping these kinds of events,

    I was just looking at the script mysqld_safe which is installed as part of all mariadb/mysql installs, and one of its interesting features is that it traps attempts to kill the process and either restarts the process or invalidates the action.

    The script does a lot more, so for your purposes if you wanted to inspect and use this specific functionality you might want to make a copy and remove everything irrelevant to what you want. The code is also very modularized for flexibility (and best practice) so you'd have to do a little sleuthing following the execution plan(unless you ran it through a stepping debugger).

    The code is extensively commented for easy understanding.

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  7. #7
    Join Date
    Sep 2012
    Posts
    5,230

    Default Re: bash control-c trap

    Quote Originally Posted by lord_valarian View Post
    The code should work.
    It also works here using template you provided. Please provide actual minimal script that shows this problem and information about your environment and how you start this script.

    Quote Originally Posted by hcvv View Post
    If I understand you correct, you have a script where you trap the Crtl-C interrupt. Inside the script you run a program. When the program is running, you do a Crtl-C. That interrupts the program (not the script). The program seems to exit as the result of the interrupt. End of story.
    Ctrl-C is sent to all processes in current foreground process group. So shell should get it - assuming that shell script is the command that is actually started. Unfortunately original post lacks any detail so any answer is nothing more than guesswork.

  8. #8

    Default Re: bash control-c trap

    I'm studying the answers here and this code.


    Code:
    #!/bin/bash 
    
    # Enable job control
    set -m
    
    while :
    do
        read -t 10 -p "input> " input
        [[ $input == finish ]] && break
    
        # set SIGINT to default action
        trap - SIGINT
    
        # Run the command in background
        bash -c "$input" &
    
        # Set our signal mask to ignore SIGINT
        trap "" SIGINT
    
        # Move the command back-into foreground
        fg %-
    
    done

  9. #9
    Join Date
    Sep 2012
    Posts
    5,230

    Default Re: bash control-c trap

    Quote Originally Posted by lord_valarian View Post
    and this code.
    Is this the question? In this case you forgot to ask it.

  10. #10

    Default Re: bash control-c trap

    Quote Originally Posted by arvidjaar View Post
    Is this the question? In this case you forgot to ask it.
    Sorry, I had to leave at that time. The idea, I send clamscan to the background and noting the PID number. When I press control-c, I kill clamscan and set a flag. Also, I'm trying to understand parent-child concept in bash scripts.


    Code:
    trap "kill $clamscan_PID;trap_flag=controlc" SIGINT
    
    clamscan -r \ 
    clamscan_PID=#!
    So, I'm not fully understanding what is going on when I press control-c with a trap and child-process is running. My original code is very complex. It will confuse the problem for me and anyone helping. This is the problem in it's most basic form.

    How do I get this to work? The trap prevents control-c from exiting the script. Either controlc cancels clamscan, but the script still runs. Else, control-c is doesn't cancel the script and the clamscan process. So, I will need to kill clamscan in the trap. If it's exiting on clamscan process not returning to the code, I need to change return code(exit 0). Low on time, again.


    Code:
    #!/bin/bash
    trap_key=false
    
    # capture the interrupt control-c
    trap "kill $clamscan_PID;trap_key=controlc" SIGINT
    
    clamscan -r /
    
    if [ trap_key = 'controlc' ]; then
        printf "clamscan interrupted\n"
    else
        printf "clamscan completed
    fi

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •