Trap CTRL-C and background process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trap CTRL-C and background process
# 8  
Old 04-22-2010
Let's forget about your traps for a moment (which aren't well implemented, I'm sorry to say).

When you launch that script from an interactive shell prompt, it, all of its non-interactive subshells, and any external commands it executes will run as part of the same process group. When you press ctrl-c, the system will send SIGINT to every single process in that terminal's foreground process group (which includes scp). You should expect scp to be killed.

Except for one very important thing: when running a process as part of an asynchronous list (backgrounded, as you're doing with scp), it inherits a signal mask which ignores SIGINT (and I think SIGQUIT as well). So, if scp is running asynchronously, and such processes are setup by their shells to ignore SIGINT, why is it terminating when it receives SIGINT? Most probably, scp is modifying its inherited sigmask.

I don't know which implementation you are using, but a quick peek at OpenSSH's scp.c turns up a couple instances of "signal(SIGINT, killchild);".

If I am correct, and the issue is that scp is modifying its sigmask to terminate upon receipt of SIGINT, try running it in a different process group. This way, when ctrl-c sends SIGINT to every process in the foreground group, scp will not be signaled, as it's not a member. One way of accomplishing this is to invoke an interactive shell from your script.

Code:
# Instead of joining the current process group...
scp large.tar.gz server:/tmp/ &

# ... let's start another process group
sh -ic 'scp large.tar.gz server:/tmp/' &


Regarding your traps, note that since SIGINT is being sent directly to each process in the process group, you cannot use sh signal handlers to modify the signal handling behavior of other processes (including subshells). The only signal handling modification that you can make that is inheritable, by subshells and external utilities, is setting a signal to be ignored (using `trap '' SIGINT`).

Also, in your code, once the signal handler is entered, to handle the arrival of a particular signal, the script is stuck in the signal handler until a different signal interrupts the wait (since the current signal will be ignored so long as its handler is executing); at which point, if the newly-received signal is being trapped, the script is again stuck, in yet another instance of the signal handler. If you send the same signal more than once, you'll see that it only prints the message the first time.

Assuming you actually even need a signal handler to accomplish what you're trying to do (which you don't, if you just want to set some signals to be ignored), it would be a good idea to remove the wait from signal_exit and use a while loop in testFunction to resume waiting if wait is interrupted by a signal.

I sincerely hope that this post was helpful to you.

Regards,
Alister
# 9  
Old 04-23-2010
Hi Alister,
thanks for this complete explanation.

I tired running scp in another process group as you mention, i.e.

Code:
sh -ic 'scp large.tar.gz server:/tmp/' &

but I have the impression that it does the exact opposite of what I expected: when I hit ctrl+c,
- the scp stops
- rest of the script is executed ( echo "COPY END")
- my trap function "signal_exit" is not executed

Smilie
# 10  
Old 04-23-2010
Hi, Robert:

For the moment, I suggest forgetting about the trap and simplify your script as much as possible. Whether or not you have a trap in place, every process in the terminal's foreground process group will be sent SIGINT when you press control-c. Your trap in a sh cannot prevent that.

While testing with the following, I discovered a flaw in my proposed solution.

Code:
#!/bin/bash

echo COPY START
# sh -ic 'scp large.tar.gz server:/tmp/' &
sh -ic 'sleep 5' &
sleep 2
ps -t $(tty) -o pid,ppid,pgid,stat,command
wait
echo COPY END


My results:
Code:
$ ./robertford.sh
COPY START
  PID  PPID  PGID STAT COMMAND
 3896   478  3896 Ss   login -pf xxxxx
 3897  3896  3897 S    -bash
 5278  3897  5278 S    /bin/bash ./robertford.sh
 5279  5278  5279 S+   sleep 5
 5282  5278  5278 R    ps -t /dev/ttyp2 -o pid
COPY END

Note that sleep is in a different process group 5279 while the shell script invoked from the command line is in process group 5278. Unfortunately, the "+" in the STAT column means that it's in the foreground group, so it will receive the SIGINT still. I overlooked that in my earlier reply. I'm sorry.

By the way, the fact that the original script's process group is no longer in the foreground is the reason that your trap was not triggered; that sh was not sent SIGINT.

Have you checked scp's logs? While my proposed solution did not work, I believe that the analysis of the problem is sound. scp logs should indicate if it is aborting the transfer due to a signal (if necessary, set verbosity to maximum).

I look forward to hearing how this turns out; so if you resolve it (whether with my recommendation, someone else's, or your own insight), please post back with problem/solution details.

Regards,
Alister

Last edited by alister; 04-23-2010 at 05:50 PM..
# 11  
Old 04-23-2010
Would
Code:
/bin/bash -lc 'command goes here'

not work? -l == act as if it were a login, meaning it creates a new process tree?
I believe it calls setsid() and creates a new separate process group.
My bash is v 2.05 which does not support the -l option. Correct me if I'm wrong on this, please.
# 12  
Old 04-23-2010
Tinkering a bit more with my prior attempt at a workaround...

Since by default an interactive shell will put a pipeline in a new process group and make that new group the foreground group, if we want to prevent the progress group created by the -c argument to the subshell from taking the foreground, it must be backgrounded. However, without a wait, there will be nothing for the main script to wait on. So, the following may be the best we can do:

Code:
#!/bin/bash

echo COPY START
# sh -ic 'scp large.tar.gz server:/tmp/ & wait' &
sh -ic 'sleep 15 & wait' &
ps -t $(tty) -o pid,ppid,pgid,stat,command
wait
echo COPY END

Test run:
Code:
$ ./robertford.sh
COPY START
[1] 5418
  PID  PPID  PGID STAT COMMAND
 3896   478  3896 Ss   login -pf xxxxx
 3897  3896  3897 S    -bash
 5415  3897  5415 S    /bin/bash ./robertford.sh
 5416  5415  5416 S+   sh -ic sleep 15 & wait
 5418  5416  5418 S    sleep 15
 5420  5415  5415 R    ps -t /dev/ttyp2 -o pid
^C
COPY END

The interactive subshell will execute in a different process group and will become the foreground process group. control-c will be sent to it and any other members of that group. However, the backgrounded pipeline (in this case, sleep 15, ultimately it should be your scp command) is run in another process group which is at last not in the foreground.

Cheers,
Alister

---------- Post updated at 12:39 PM ---------- Previous update was at 11:36 AM ----------

Quote:
Originally Posted by jim mcnamara
Would
Code:
/bin/bash -lc 'command goes here'

not work? -l == act as if it were a login, meaning it creates a new process tree?
I believe it calls setsid() and creates a new separate process group.
My bash is v 2.05 which does not support the -l option. Correct me if I'm wrong on this, please.
I tested it with the same script I used in my previous post, but it doesn't even create a new process group. I'm assuming that without the -i option, -c renders it a non-interactive shell, despite the -l/--login options (I tried both).

I believe that session creation for interactive use is handled by the login process before it execs the user's shell.

A quick peek at the process list of a few systems, using different default login shells (debian-bash, osx-bash, openbsd-ksh) shows:

debian and osx: -bash login shells are not session leaders (each terminal with a logged in user has a login process associated with it that is the session leader).

openbsd: -ksh login shells are session leaders (there's no login process for those terminals).

Without looking at the source or examining a process trace, one cannot be absolutely certain, but it seems to me that differences in login implementations determine whether or not a user's login shell will be a session leader. The shell's themselves never seem to attempt to become a session leader (in the openbsd case, `ksh -l` will not yield a session leader, which is why I assume it's set up that way by the login process that exec'd it).

Regards,
Alister

Last edited by alister; 04-23-2010 at 12:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question