Sponsored Content
Top Forums UNIX for Advanced & Expert Users Trapping a kill command sent to a script and using it to send an EOF to a subprocess before killing Post 302880587 by jim mcnamara on Friday 20th of December 2013 07:13:00 PM
Old 12-20-2013
You need a source of active subprocesses. ptree is available in Solaris, for example.
Linux has /proc/[pid]/ppid. pstree uses this to draw ascii "graphics".

So what OS?

The bash trap statement you want: a function + two trap statements

Code:
zap()  # some method of getting all active children/grandchildren, etc. Assume a file
{
    # turn off trap
    trap - SIGTERM 
    # kill the kids
    me=$$
    while read pid
    do
        if [ $$ -ne  $pid ] ; then   # if active kill the process
           kill -0 $pid && kill $pid
        fi
   done < a_file_with_childrens_pids
}

# enable zap
trap ' zap ' SIGTERM

Bash kill builtin also supports using jobs, so you can use the jobs command to list active children - if they do not have descendants.

Notice there is a very small possibility of a race condition when several SIGTERM (default signal for kill) signals are received at once. In that case zap() will not complete.

You need a way to enumerate children. You can remember their pids in an array or a file or some other object maybe.
If one of the children is "extinct", you did not rembmer that, and the pid is being used by some hapless process - then you have problems. Another race condition. Especially if this stuff runs as root. On linux or solaris you can evaluate the correctness of the pid by querying proc or getting the uid. - maybe like what exe is the potential kid running. If root is running zap() and you run, be sure to keep a tight handle on current active descendant processes. Or you could clobber your own payroll job.

Last edited by jim mcnamara; 12-20-2013 at 08:20 PM..
 

10 More Discussions You Might Find Interesting

1. HP-UX

Trapping the rm command in UNIX

Hi All, Whoever types rm in the command prompt I need to trap the username, the ip address,timestamp and the rm filename(which file they are removing) and write it to the log file. could anyone help me?. Advanced thanks Suma (1 Reply)
Discussion started by: sumas
1 Replies

2. Shell Programming and Scripting

Getting value of variable set in subprocess script

I am writing a shell script that executes another script by fetching it over the network and piping its contents into sh (ftp -o - $script | sh; or wget -O - |sh). Since this bypasses putting the script on the filesystem, this means I can't source the script directly (using . ), but rather it... (1 Reply)
Discussion started by: hadarot
1 Replies

3. Shell Programming and Scripting

Killing an Xterm while leaving subprocess alive...

Hi, I'm not quite understanding what I'm doing (happens often). This pseudocode works: #!/bin/pseudoksh function kill_parent { when i_want_to ; do sleep 2 kill -TERM $PPID exit done } kill_parent & ssh remote_host sh <<-EOF ... (0 Replies)
Discussion started by: mschwage
0 Replies

4. Shell Programming and Scripting

command << EOF(dont want to call other script)

Dear Freinds, Help needed in input redirection . My problem is as follows.. I have a shell script as follows which calls another gnuplot script . datagen.sh #!/bin/ksh gnuplot plot_I.plt In the above file I am calling another file called plot_I.plt which reside in the same... (4 Replies)
Discussion started by: user_prady
4 Replies

5. UNIX for Advanced & Expert Users

How to kill top command using script?

Hi all, I am using top command in my script to redirect output to temp file. I used kill -9 `ps -ef|grep top|grep -v grep|awk '{print $2}'` to kill top command in my script, but it is not working? Can you please tell how to kill top command in my script? (4 Replies)
Discussion started by: johnl
4 Replies

6. Shell Programming and Scripting

Trapping Enter command !!

Hi I have written a script which works well .. It's divided into a series of jobs one running after another But on pressing the enter key the whole script goes haywire .. I want a way to trap this enter command and echo it as invalid input .. Any suggestions highly appreciated... Thanks :) (2 Replies)
Discussion started by: ultimatix
2 Replies

7. UNIX for Dummies Questions & Answers

Send output of grep as input of kill command

I would appreciate any help. I need to run 'ps -ef | grep 'process', get the process id and kill that process. I have got this far: - Get pid using ps -ef | awk '/process/{ print $2}' after this I'm kind of stuck.. - Use pipe to redirect the output to kill pid=ps -ef | awk '/bmserver/{... (2 Replies)
Discussion started by: foxtron
2 Replies

8. Shell Programming and Scripting

Perl script to kill the process ID of a command after a certain period

All, I am trying to build a script in perl that will alllow me to pass the IP address to a ping command and redirect the output to a file and then kill that process after a certain period of time. let's say, I call my script ping.pl, I would like to be able to run it like this for example :... (7 Replies)
Discussion started by: Pouchie1
7 Replies

9. Shell Programming and Scripting

Kill idle script is killing unnecessarly

Hi All,I have a problem with my kill idle script.my script is supposed to kill the user sessions which are idle for more than 2 hours.But is is killing the sessions which are idle for less than 2 hrs also.I dont know the exact time after which the script is killing,but it is less than 2 hours i am... (3 Replies)
Discussion started by: prabhu_kumar
3 Replies

10. Shell Programming and Scripting

Script which uses “kill -9” command

i have one script which uses “kill -9” command. That prevents from getting the process core dumps. Apparently once tomcat lands in a confused state, we seem to have no other option, other than Kill -9. is there any other way to get rid of this. Script: sleep 2 PID=`ps -ef | grep "^tomcat... (3 Replies)
Discussion started by: Amrutayan09
3 Replies
zap(1)								  MySQL database							    zap(1)

NAME
zap - a perl script used to kill processes USAGE
/usr/bin/mysql_zap [-signal] [-?Ift] pattern SYNOPSIS
zap [-I|-?] [-f] [-t] DESCRIPTION
zap supports by executing -I|-? info -f force -t test NOTE
If -f isn't given, ask user for confirmation for each process to kill. If signal isn't given, try first with signal 15 and after that with signal 9. If -t is given the processes is only shown on stdout. SEE ALSO
isamchk(1), isamlog(1), mysql(1), mysqlaccess(1), mysqladmin(1), mysqld(1), mysqld_multi(1), mysqld_safe(1), mysqldump(1), mysql_fix_privi- lege_tables(1), mysqlshow(1), perror(1), replace(1) For more information please refer to the MySQL reference manual, which may already be installed locally and which is also available online at http://www.mysql.com/doc/en/ BUGS
Please refer to http://bugs.mysql.com/ to report bugs. AUTHOR
Ver 1.0, distribution 4.0.24 Michael (Monty) Widenius (monty@mysql.com), MySQL AB (http://www.mysql.com/). This software comes with no war- ranty. Manual page by L. (Kill-9) Pedersen (kill-9@kill-9.dk), Mercurmedia Data Model Architect / system developer (http://www.mercurme- dia.com) ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +--------------------+------------------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +--------------------+------------------------------------+ |Availability | SUNWmysqlr, SUNWmysqlu, SUNWmysqlt | +--------------------+------------------------------------+ |Interface Stability | External | +--------------------+------------------------------------+ NOTES
Source for mysql is available on http://opensolaris.org. MySQL 4.0 19 December 2000 zap(1)
All times are GMT -4. The time now is 04:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy