Visit Our UNIX and Linux User Community


Trap CTRL-C and background process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trap CTRL-C and background process
# 1  
Old 04-22-2010
Trap CTRL-C and background process

Hello,
I have a script which copies via scp several large files to a remote server. What I want is that even if someone hits CTRL-C, the scp commands continues till the end.

Here is what I wrote
Code:
#! /bin/bash

function testFunction
{
    echo "COPY START"
    scp large.tar.gz server:/tmp/ &

    myPID=$!
    echo "Waiting for PID[$myPID]"
    wait $myPID
    echo "COPY END"
}

# Trap TERM, HUP, and INT signals to wait for the scp
trap "signal_exit" TERM HUP INT

function signal_exit
{
    echo  "CTRL-C trapped. Waiting for $myPID"
    wait $myPID
}

testFunction

The CTRL-C signal is correctly trapped but the problem is that the scp command is interrupted. Only a part of my file is copied.

What am I doing wrong ?

Thanks,
R.F
# 2  
Old 04-22-2010
CTR-C corresponds to SIGTERM.
Use
Code:
TRAP

command to trap that signal.
# 3  
Old 04-22-2010
Quote:
Originally Posted by devtakh
CTR-C corresponds to SIGTERM.
Use
Code:
TRAP

command to trap that signal.
Do you mean just adding SIGTERM to the trap command:

Code:
trap "signal_exit" TERM HUP INT SIGTERM

It doesn't work either.
# 4  
Old 04-22-2010
a) he's already trapping it
b) Ctrl-C is SIGINT, not SIGTERM

However, I could not reproduce that behaviour. What version of bash are you using?
# 5  
Old 04-22-2010
Quote:
Originally Posted by pludi
However, I could not reproduce that behaviour. What version of bash are you using?
Code:
$ bash --version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

# 6  
Old 04-22-2010
Try disabling the key and remove it from the trap statement.

Code:
stty intr ""

# 7  
Old 04-22-2010
MySQL

Quote:
Originally Posted by RobertFord
Hello,
I have a script which copies via scp several large files to a remote server. What I want is that even if someone hits CTRL-C, the scp commands continues till the end.

Here is what I wrote
Code:
#! /bin/bash
 
function testFunction
{
    echo "COPY START"
    scp large.tar.gz server:/tmp/ &
 
    myPID=$!
    echo "Waiting for PID[$myPID]"
    wait $myPID
    echo "COPY END"
}
 
# Trap TERM, HUP, and INT signals to wait for the scp
trap "signal_exit" TERM HUP INT
 
function signal_exit
{
    echo  "CTRL-C trapped. Waiting for $myPID"
    wait $myPID
}
 
testFunction

The CTRL-C signal is correctly trapped but the problem is that the scp command is interrupted. Only a part of my file is copied.

What am I doing wrong ?

Thanks,
R.F
Let try this..

Code:
trap '' 0 2 3 15
scp large.tar.gz server:/tmp/ & pid=$! ; wait $pid


Previous Thread | Next Thread
Test Your Knowledge in Computers #130
Difficulty: Easy
The original Unix code was developed by AT&T.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

--killing backround Procs spawned from the parent script with Ctrl+C trap

Hello: Am trying to understand why the method #2 works but method #1 does not. For both methods, sending CTRL+C should kill both the Parent script & all of the spanwd background procs. Method #1: ========================== #!/bin/sh ctrl_c() { echo "** Trapped CTRL-C" ... (3 Replies)
Discussion started by: gilgamesh
3 Replies

2. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies

3. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

4. Shell Programming and Scripting

Help with getting a Ctrl-C trap working w/ a piped tail -f...

Hi All, Although each line below seems to work by itself, I've been having trouble getting the Control-C trap working when I add the "|perl -pe..." to the end of the tail -f line, below. (That |perl -pe statement basically just adds color to highlight the word "ERROR" while tailing a log... (2 Replies)
Discussion started by: chatguy
2 Replies

5. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

6. Shell Programming and Scripting

trap CTRL-C problem

I am trying to trap CTRL-C, now the program I call has it's own exit message, I think this is the problem .. This is what I have now : function dothis { echo 'you hit control-c' exit } function settrap { trap dothis SIGINT } settrap until false; do ./ITGRecv.exe doneDoing this I... (2 Replies)
Discussion started by: Pmarcoen
2 Replies

7. UNIX for Advanced & Expert Users

trap ctrl c in shell script

how to trap the ctrl c in unix shell script my script is running in while loop it should not be terminate with ctrl c. if i press ctrl c while running script it shloud ignore the same. please healp.......... thanks in advance (2 Replies)
Discussion started by: arvindng
2 Replies

8. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies

9. UNIX for Dummies Questions & Answers

problems with ctrl-z, to switch foreground, background

my shell is /sbin/sh. i added stty susp '^Z' with the intention of being able to switch between foreground and background. but the result was strange. i had 2 servers. one is sun the os is 8 and the other is hpux v11. both of them had the same shell. but on hpux, it works perfectly fine while... (9 Replies)
Discussion started by: yls177
9 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies

Featured Tech Videos