Joined: 17 Feb 2015
|Posted: Fri Mar 06, 2015 9:59 pm Post subject: Kill/ Trap command not working
|I have bash script where it calls kill command
now the script that is mapped to this pid will capture the signal SIGTERM sent by the kill command and performs stop all its sub-processes before exiting.
Now the 1st script that called the kill command , will go in a while loop for a waiting time of some XXX seconds to check if the pid is killed or not. This is done by calling kill -0 pid. If it is killed, it breaks from the loop and exit otherwise throw and error and exit.
The problem I'm facing is: The 2nd script captures the SIGTERM signal only after the first 1st script ends. It means, the 1st script goes in while loop and once the waiting time is crossed it throws an error exits first. Then the second script captures the SIGTERM signal and performs the action and exits.
When I gave high priority for pid of second script using renice thinking It will make the CPU to execute 1st script as soon as the signal is trapped, but it failed to do so.
How can I make the script that gets the signal to complete its action before the parent script ??
Template of my first script:
variables timeout, interval and waiting time are set to some initial values
sleep for few seconds
while waiting_time < timout:
check if kill -0 pid
if it gets 0 go for sleep and increasing waiting time value
if waiting_time > timeout and still kill -0 pid returned 0 value:
|trap function_name SIGTERM
function_name does the following this:
kill whatever the pids that are started by it
and exit 0
Now if I give
, the signal is not captured by the script
|kill process_id_of_second_script |
kills the process but it is not killing the processes that are created by it.
|kill -9 process_id_of_second_script |
And if I kill any of the processes created by the second script, it is created again in different process id.
How to make the script to accept the signal immediately when the kill command is called ?