The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash problem edgehead Shell Programming and Scripting 3 11-15-2008 04:34 AM
Cntl+z Trap is not detecting ??? Help required to add a trap detection ??? frozensmilz Shell Programming and Scripting 1 10-08-2008 06:15 AM
how to trap unix signal if the process killed/interupt occured in bash... manas_ranjan Shell Programming and Scripting 15 09-23-2007 08:47 AM
Building a better mouse trap, or How many lines of code does it take to trap a mouse? mph Shell Programming and Scripting 4 04-06-2006 04:02 PM
Trap command problem superprogrammer Shell Programming and Scripting 3 06-06-2005 09:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2008
jsabino jsabino is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
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 (permalink)  
Old 11-14-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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 (permalink)  
Old 11-17-2008
jsabino jsabino is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
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 (permalink)  
Old 11-17-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0