samedi 7 mars 2015

execute the script that received SIGTERM before the parent script that sent the KILL command


I have bash script where it calls kill command



kill pid


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 script ends. It means, the first 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:



kill pid

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
else break

if waiting_time > timeout and still kill -0 pid returned 0 value:
print error


2nd script:



openvt -f -s -w arguments
trap function_name SIGTERM

function_name does the following this:

Stop whatever the pids that are started by it

and exit 0


Aucun commentaire:

Enregistrer un commentaire