Deleting all the parallel processes launched when the main script receives a ctrl+c


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting all the parallel processes launched when the main script receives a ctrl+c
# 1  
Old 08-27-2012
Deleting all the parallel processes launched when the main script receives a ctrl+c

Hi,

I have a shell script that creates 2 parallel processes. When I press ctrl+c, i want the parallel process to get killed as well.


Code:
#!/bin/bash



cmd1="script1.py" 
cmd2="script2.py" 


${cmd1} & 
pid1=$!
echo ${pid1} 


${cmd2} & 
pid2=$!
echo ${pid2} 


trap 'kill ${pid1} ${pid2}; echo "this works"' INT

When I use the ps command and check, I dont see the pid1 and pid2 getting killed? Kindly help me out.

Thanks.

Last edited by methyl; 08-27-2012 at 08:10 PM.. Reason: please use code tags
# 2  
Old 08-27-2012
The script will run and quit without receiving any sort of interrupt, leaving the processes behind.
# 3  
Old 08-27-2012
Quote:
trap 'kill ${pid1} ${pid2}; echo "this works"' INT
Your trap command is faulty. It does not mention what signals it is trapping and has problems with the single quote characters:

Maybe try this. Note that I have twiddled with your quote characters so that the values of $pid1 and $pid2 get expanded. Any variable between single quote characters does not get expanded:
Code:
trap INT "kill ${pid1} ${pid2}; echo 'this works'"


Please remember to post what Operating System and version you have and what Shell you are running (okay it is bash). Without this basic information any response is pure guesswork.

Last edited by methyl; 08-27-2012 at 08:25 PM..
# 4  
Old 08-27-2012
@Methyl,

This is the info of the system that I am using:
Code:
Linux version 2.6.16.60-0.58.1.1290.1.PTF.600981-smp (geeko@buildhost) (gcc version 4.1.2 20070115 (SUSE Linux)) #1 SMP Wed Dec 2 12:27:56 UTC 200

And when I change the trap according to what you have posted, I get the following:
Code:
trap: kill 5218 5219; echo 'this works': invalid signal specification

---------- Post updated at 03:37 PM ---------- Previous update was at 03:35 PM ----------

@Corona,

Yeah, that is exactly what is happening. I am trying to find a solution to that. Since I am fairly new to coding in linux, I am not sure how to kill the subprocesses.

Last edited by methyl; 08-27-2012 at 09:00 PM.. Reason: code tags for readability
# 5  
Old 08-27-2012
Anybody out there with matching SUSE who can try this trap and correct the syntax?
# 6  
Old 08-27-2012
Quote:
Originally Posted by sana.usha
Yeah, that is exactly what is happening. I am trying to find a solution to that. Since I am fairly new to coding in linux, I am not sure how to kill the subprocesses.
Your process doesn't get a signal unless it's sent one, so you're looking for the wrong thing.

Try trap "do something" EXIT instead.
# 7  
Old 08-27-2012
That system is pretty old. I've got SUSE 12.x running on one of my boxes and this works for me:

First a little 'run it' script that does something (probably sleep):

Code:
#/usr/bin/env ksh 
# (could be bash)

echo "$$: running $@"
"$@"
echo "$$: done"
exit 0

If we don't see the done message, we assume it was killed. I did add a trap to run-it to verity, but the timing generated messages after the shell prompt and it was confusing.

Code to invoke the processes. the big problem, as Corona pointed out, is that the initial script doesn't wait for the asynch processes it just exits. Wait added at end to allow it to actually trap the interrupt.

Code:
#!/bin/bash

run_it sleep $1 &
pid1=$!
echo ${pid1}  
  
run_it sleep $1 &
pid2=$! 
echo ${pid2} 
  
trap 'kill -15 ${pid1} ${pid2}; echo "killed: $pid1 $pid2"' INT
wait   # very important to wait for the asynch processes if they are to be killed with ctl-c

The quoting is as it was on the original post; the $pid1 and $pid2 variables are expanded when the trap is executed. This allows the trap to be set before the processes are invoked; this allows for the starting several sets of processes, killing the most recently started pair when interrupted, without having to reset the trap for each. There are arguments pro and con for this I am sure, but not really the point here.

Finally, I always thought the syntax for trap is:
Code:
trap [-p] 'command' signal [...]

Specifying the signal before the command string would result in a syntax error.

Last edited by agama; 08-27-2012 at 10:52 PM.. Reason: clarification
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

(bash) Script Processes in Parallel

Hello all, I tried to parralise my treatments but after a while 'ps -ef' display all child process <defunct> (zombie) Parent bash script to process all files (>100000) in directory: for filename in /Data/*.txt; do ./child_pprocess.sh $filename & done exit(0)I understand that the... (1 Reply)
Discussion started by: namnetes
1 Replies

2. Shell Programming and Scripting

Keep up constant number of parallel processes

Hi guys, I am struggling with adapting my script to increase the performance. I created a ksh script to process a lot of files in parallel. I would like to know how can I do in such a way that a constant number of processes is always up (until all is finished). What I have is (not actual... (8 Replies)
Discussion started by: lurkerro
8 Replies

3. Shell Programming and Scripting

How to handle CTRL+Z or CTRL+C in shells script?

Hi, while executing shell script, in the middle of the process, if we kill the shell script( ctrl+z or ctrl+c), script will be killed and the files which using for the script will be in the folder. How to handle those scenarios. Is there any possibilities, if user breaks the script, I need to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

4. Shell Programming and Scripting

Question about a basic shell script: How to make sure it receives only one input?

Hello all! I am very new to shell and Linux in general (I just started 2 days ago), I am trying to write a script that adds the size of the directories and files in a given directory and displays messages if the user puts in something wrong. I think I have covered all the possible problems except... (3 Replies)
Discussion started by: 2358
3 Replies

5. Shell Programming and Scripting

Parallel processes to INC- and DEC-rement shared counter

QUESTION: How do I run processes in parallel, so that the counter (in counter.txt) would vary in value (instead of just "0" and "1")? That is, how to not sequentially run inc.sh and dec.sh? The shared counter (a single number starting as 0) is in a file counter.txt. counter.sh is (supposed to... (2 Replies)
Discussion started by: courteous
2 Replies

6. Shell Programming and Scripting

Retention of Variable Value when a script is called by different processes in parallel- Linux 2.6.9

Hi, I have a generic FTP script which will be called by 28 different processes in parallel (through a GUI tool) may or may not be at the exact moment (there could be a delay of about a minute or so). ./FTP.ksh 1 (1 through 28) This script after importing file from remote m/c... (1 Reply)
Discussion started by: dips_ag
1 Replies

7. UNIX for Advanced & Expert Users

Which script/shell/program launched a given script?

Dear gurus, I was wondering if anybody can help. Is it possible to distinguish whether a script was launched from a shell (interactively) or was called from another script? I basically need to do the following: if the script was run from the command line, I want to enable logging of the... (2 Replies)
Discussion started by: Snakeye
2 Replies

8. Shell Programming and Scripting

script run when executable is launched

I need to know how to have a BASH script run every time Firefox is launched, what is the simplest way to do this? (1 Reply)
Discussion started by: glev2005
1 Replies

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

10. Shell Programming and Scripting

How to run processes in parallel?

In a korn shell script, how can I run several processes in parallel at the same time? For example, I have 3 processes say p1, p2, p3 if I call them as p1.ksh p2.ksh p3.ksh they will run after one process finishes. But I want to run them in parallel and want to display "Process p1... (3 Replies)
Discussion started by: sbasak
3 Replies
Login or Register to Ask a Question