Bash Trap Problem?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Trap Problem?
# 1  
Old 11-14-2008
Bash Trap Problem?

Hey all, I'm a fairly new shell scripter (been writing some very basic stuff for a couple of years once in a blue moon).

I have the need to start 2 or 3 processes from within a bash script when it's run then have the script kind of hang out and wait for the user to ctrl+c and when that happens the 2 or 3 running processes should be terminated and the script should continue.

I have a lot of the script written but am stuck at this point. I think I need to grab the PID #'s of the running processes using $$ or something then when CTRL-C is hit, send them a kill type signal but I've read a lot and message around with the trap command and can't seem to get it working right. Basically it would like like this.

./run_script.sh

Process 1 starts
Process 2 starts
Process 3 starts

Wait for user to hit [CTRL+C]...

[ctrl+c] is pressed

Process 1 ends
Process 2 ends
Process 3 ends

Script continues..

Any help you can give would be appreciated.
Thanks!
JS
# 2  
Old 11-14-2008
Code:
#!/bin/sh
a=0
b=0
c=0
trap "kill $a $b $c", INT
script1.sh &
a=$!
script2.sh &
b=$!
script3.sh &
c=$!
wait
# the trap will resume here if the jobs all complete early, the script
# go on from  here as well
# .........more commands
exit

# 3  
Old 11-17-2008
Great, thanks Jim, I'll try that. As a follow-up, your code assumes that all 3 of these processes will be running. In actuality, the beginning of my script is a bit interactive in that it first asks the user whether or not they want process 1/2/3 to run.

Having said that, I'm thinking that this trap would try and kill a process that may or may not be running, how might it be modified to first check if that particular process is running (e.g., the user answered Yes to that question at the beginning) and if so, then issue the kill?

Regards,
JS
# 4  
Old 11-17-2008
Use a case statement, keep track of the number of processes you intend to create, based on user input: note move the trap statement can be anywhere before the wait statement, that way you know how many processes have been created.
Code:
t=1
case $t in
  1) trap  "kill $a ", INT       ; break;;
  2) trap  "kill $a $b ", INT    ; break;;
  3) trap  "kill $a $b $c", INT  ; break;;
  *) exit 2;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Solaris 10 boot problem - ERROR: Last Trap: Fast Data Access MMU Miss

Hello, We have a T5140 server with Solaris 10 and its suddenly throwing "segmentation core" when I login into the server and not showing any output for commands like df, mount etc. so I had to reboot the server to fix this issue. Please note that there's no boot disk mirroring. But... (2 Replies)
Discussion started by: prvnrk
2 Replies

2. Shell Programming and Scripting

Bash - trap error file not found

Hello. In bash, is there a way to trap error "file not found" when a script call another script which is not found; then abort. Example ( part of script running with -x option set) : + return 0 + RETURN_CODE=0 + ] + /root/bin/200_yast_install/00_reset_yast_install bash:... (5 Replies)
Discussion started by: jcdole
5 Replies

3. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

4. Shell Programming and Scripting

How to trap set -o nounset in bash

I have a bash script using "set -o nounset" to prevent unset variables. However I have created a trap to run some cleanup options upon exit of the script which works fine for CTRL-C, etc. but if it hits and unset variable the trap does not run and the script bails out without having tidied up... (3 Replies)
Discussion started by: jelloir
3 Replies

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

6. Shell Programming and Scripting

Trap Command Problem

Hi All , Could you please help me in resolving the below problem. I have 2 Script file say Scrip1.sh Script2.sh. Initially Script1.sh is started executed and inside the script1 , SIGINT is trapped to a separate cleanup process and exit. While Script1.sh is started executing ,... (1 Reply)
Discussion started by: sundar_ranga
1 Replies

7. Shell Programming and Scripting

Record the Signal Type or Number in Bash Trap function

In my Bash script I have an exit/cleanup function in a trap statement like: trap exitCleanup 1 2 3 6 15 25 Is there anyway to capture which signal # has occurred to record in a log file. Please note I am trying to avoid something like: trap 'mySignal=1; exitCleanup' 1 trap... (1 Reply)
Discussion started by: ckmehta
1 Replies

8. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

9. Shell Programming and Scripting

how to trap unix signal if the process killed/interupt occured in bash...

hey champs, I have a process running.......i have to catch/trap the signal when the process is being interupted/killed (kill -9 pid) option...... how can i achieve the same thru my process........ let my process is a.sh and it supposed to take 13 mins to complete, but due to some problem ,... (15 Replies)
Discussion started by: manas_ranjan
15 Replies

10. Shell Programming and Scripting

Trap command problem

I am having a unix script which in turns calls another unix script. Both are creating some temp files in /tmp which are supposed to be deleted after the end of each run In the child script if I have statement like trap "rm -f $messagefile" EXIT A similar statement is there in parent... (3 Replies)
Discussion started by: superprogrammer
3 Replies
Login or Register to Ask a Question