Merge pull request #6 from deajan/betterKillChilds

Better kill childs works flawlessly
This commit is contained in:
Orsiris de Jong 2016-04-01 21:20:09 +02:00
commit 79ec8668e4
3 changed files with 138 additions and 69 deletions

View File

@ -8,7 +8,7 @@ PROGRAM_VERSION=2.0-pre
PROGRAM_BUILD=2016033101 PROGRAM_BUILD=2016033101
IS_STABLE=no IS_STABLE=no
## FUNC_BUILD=2016033103 ## FUNC_BUILD=2016033105
## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode ## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode
@ -159,11 +159,20 @@ function KillChilds {
done done
fi fi
# Try to kill nicely, if not, wait 30 seconds to let Trap actions happen before killing # Try to kill nicely, if not, wait 15 seconds to let Trap actions happen before killing
if [ "$self" == true ]; then if [ "$self" == true ]; then
kill -s SIGTERM "$pid" || (sleep 30 && kill -9 "$pid" &) if [ "$_DEBUG" == "yes" ]; then
Logger "Killing process $pid" "NOTICE"
kill -s SIGTERM "$pid"
if [ $? != 0 ]; then
sleep 15 && kill -9 "$pid" &
return 1
else
return 0
fi
fi
fi fi
# sleep 30 needs to wait before killing itself # sleep 15 needs to wait before killing itself
} }
function SendAlert { function SendAlert {
@ -523,29 +532,36 @@ function WaitForTaskCompletion {
if [ $((($exec_time + 1) % $KEEP_LOGGING)) -eq 0 ]; then if [ $((($exec_time + 1) % $KEEP_LOGGING)) -eq 0 ]; then
if [ $log_ttime -ne $exec_time ]; then if [ $log_ttime -ne $exec_time ]; then
log_ttime=$exec_time log_ttime=$exec_time
Logger "Current task still running." "NOTICE" Logger "Current task still running with pid [$pid]." "NOTICE"
fi fi
fi fi
if [ $exec_time -gt $soft_max_time ]; then if [ $exec_time -gt $soft_max_time ]; then
if [ $soft_alert -eq 0 ] && [ $soft_max_time -ne 0 ]; then if [ $soft_alert -eq 0 ] && [ $soft_max_time -ne 0 ]; then
Logger "Max soft execution time exceeded for task [$caller_name]." "WARN" Logger "Max soft execution time exceeded for task [$caller_name] with pid [$pid]." "WARN"
soft_alert=1 soft_alert=1
SendAlert SendAlert
fi fi
if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then
Logger "Max hard execution time exceeded for task [$caller_name]. Stopping task execution." "ERROR" Logger "Max hard execution time exceeded for task [$caller_name] with pid [$pid]. Stopping task execution." "ERROR"
kill -s SIGTERM $pid KillChilds $pid
if [ $? == 0 ]; then if [ $? == 0 ]; then
Logger "Task stopped succesfully" "NOTICE" Logger "Task stopped successfully" "NOTICE"
return 0
else else
Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR" return 1
sleep 5 && kill -9 $pid
if [ $? != 0 ]; then
Logger "Could not stop task." "ERROR"
fi
fi fi
return 1 #kill -s SIGTERM $pid
#if [ $? == 0 ]; then
# Logger "Task stopped succesfully" "NOTICE"
#else
# Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR"
# sleep 5 && kill -9 $pid
# if [ $? != 0 ]; then
# Logger "Could not stop task." "ERROR"
# fi
#fi
#return 1
fi fi
fi fi
sleep $SLEEP_TIME sleep $SLEEP_TIME
@ -587,17 +603,24 @@ function WaitForCompletion {
fi fi
if [ $SECONDS -gt $hard_max_time ] && [ $hard_max_time != 0 ]; then if [ $SECONDS -gt $hard_max_time ] && [ $hard_max_time != 0 ]; then
Logger "Max hard execution time exceeded for script in [$caller_name]. Stopping current task execution." "ERROR" Logger "Max hard execution time exceeded for script in [$caller_name]. Stopping current task execution." "ERROR"
kill -s SIGTERM $pid KillChilds $pid
if [ $? == 0 ]; then if [ $? == 0 ]; then
Logger "Task stopped succesfully" "NOTICE" Logger "Task stopped successfully" "NOTICE"
return 0
else else
Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR" return 1
kill -9 $pid
if [ $? != 0 ]; then
Logger "Could not stop task." "ERROR"
fi
fi fi
return 1 #kill -s SIGTERM $pid
#if [ $? == 0 ]; then
# Logger "Task stopped succesfully" "NOTICE"
#else
# Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR"
# kill -9 $pid
# if [ $? != 0 ]; then
# Logger "Could not stop task." "ERROR"
# fi
#fi
#return 1
fi fi
fi fi
sleep $SLEEP_TIME sleep $SLEEP_TIME

View File

@ -1,4 +1,4 @@
## FUNC_BUILD=2016033103 ## FUNC_BUILD=2016033105
## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode ## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode
@ -149,11 +149,20 @@ function KillChilds {
done done
fi fi
# Try to kill nicely, if not, wait 30 seconds to let Trap actions happen before killing # Try to kill nicely, if not, wait 15 seconds to let Trap actions happen before killing
if [ "$self" == true ]; then if [ "$self" == true ]; then
kill -s SIGTERM "$pid" || (sleep 30 && kill -9 "$pid" &) if [ "$_DEBUG" == "yes" ]; then
Logger "Killing process $pid" "NOTICE"
kill -s SIGTERM "$pid"
if [ $? != 0 ]; then
sleep 15 && kill -9 "$pid" &
return 1
else
return 0
fi
fi
fi fi
# sleep 30 needs to wait before killing itself # sleep 15 needs to wait before killing itself
} }
function SendAlert { function SendAlert {
@ -513,29 +522,36 @@ function WaitForTaskCompletion {
if [ $((($exec_time + 1) % $KEEP_LOGGING)) -eq 0 ]; then if [ $((($exec_time + 1) % $KEEP_LOGGING)) -eq 0 ]; then
if [ $log_ttime -ne $exec_time ]; then if [ $log_ttime -ne $exec_time ]; then
log_ttime=$exec_time log_ttime=$exec_time
Logger "Current task still running." "NOTICE" Logger "Current task still running with pid [$pid]." "NOTICE"
fi fi
fi fi
if [ $exec_time -gt $soft_max_time ]; then if [ $exec_time -gt $soft_max_time ]; then
if [ $soft_alert -eq 0 ] && [ $soft_max_time -ne 0 ]; then if [ $soft_alert -eq 0 ] && [ $soft_max_time -ne 0 ]; then
Logger "Max soft execution time exceeded for task [$caller_name]." "WARN" Logger "Max soft execution time exceeded for task [$caller_name] with pid [$pid]." "WARN"
soft_alert=1 soft_alert=1
SendAlert SendAlert
fi fi
if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then
Logger "Max hard execution time exceeded for task [$caller_name]. Stopping task execution." "ERROR" Logger "Max hard execution time exceeded for task [$caller_name] with pid [$pid]. Stopping task execution." "ERROR"
kill -s SIGTERM $pid KillChilds $pid
if [ $? == 0 ]; then if [ $? == 0 ]; then
Logger "Task stopped succesfully" "NOTICE" Logger "Task stopped successfully" "NOTICE"
return 0
else else
Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR" return 1
sleep 5 && kill -9 $pid
if [ $? != 0 ]; then
Logger "Could not stop task." "ERROR"
fi
fi fi
return 1 #kill -s SIGTERM $pid
#if [ $? == 0 ]; then
# Logger "Task stopped succesfully" "NOTICE"
#else
# Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR"
# sleep 5 && kill -9 $pid
# if [ $? != 0 ]; then
# Logger "Could not stop task." "ERROR"
# fi
#fi
#return 1
fi fi
fi fi
sleep $SLEEP_TIME sleep $SLEEP_TIME
@ -577,17 +593,24 @@ function WaitForCompletion {
fi fi
if [ $SECONDS -gt $hard_max_time ] && [ $hard_max_time != 0 ]; then if [ $SECONDS -gt $hard_max_time ] && [ $hard_max_time != 0 ]; then
Logger "Max hard execution time exceeded for script in [$caller_name]. Stopping current task execution." "ERROR" Logger "Max hard execution time exceeded for script in [$caller_name]. Stopping current task execution." "ERROR"
kill -s SIGTERM $pid KillChilds $pid
if [ $? == 0 ]; then if [ $? == 0 ]; then
Logger "Task stopped succesfully" "NOTICE" Logger "Task stopped successfully" "NOTICE"
return 0
else else
Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR" return 1
kill -9 $pid
if [ $? != 0 ]; then
Logger "Could not stop task." "ERROR"
fi
fi fi
return 1 #kill -s SIGTERM $pid
#if [ $? == 0 ]; then
# Logger "Task stopped succesfully" "NOTICE"
#else
# Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR"
# kill -9 $pid
# if [ $? != 0 ]; then
# Logger "Could not stop task." "ERROR"
# fi
#fi
#return 1
fi fi
fi fi
sleep $SLEEP_TIME sleep $SLEEP_TIME

View File

@ -8,7 +8,7 @@ PROGRAM_VERSION=2.0-pre
PROGRAM_BUILD=2016033101 PROGRAM_BUILD=2016033101
IS_STABLE=no IS_STABLE=no
## FUNC_BUILD=2016033103 ## FUNC_BUILD=2016033105
## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## BEGIN Generic functions for osync & obackup written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr
## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode ## type -p does not work on platforms other than linux (bash). If if does not work, always assume output is not a zero exitcode
@ -149,11 +149,20 @@ function KillChilds {
done done
fi fi
# Try to kill nicely, if not, wait 30 seconds to let Trap actions happen before killing # Try to kill nicely, if not, wait 15 seconds to let Trap actions happen before killing
if [ "$self" == true ]; then if [ "$self" == true ]; then
kill -s SIGTERM "$pid" || (sleep 30 && kill -9 "$pid" &) if [ "$_DEBUG" == "yes" ]; then
Logger "Killing process $pid" "NOTICE"
kill -s SIGTERM "$pid"
if [ $? != 0 ]; then
sleep 15 && kill -9 "$pid" &
return 1
else
return 0
fi
fi
fi fi
# sleep 30 needs to wait before killing itself # sleep 15 needs to wait before killing itself
} }
function SendAlert { function SendAlert {
@ -506,29 +515,36 @@ function WaitForTaskCompletion {
if [ $((($exec_time + 1) % $KEEP_LOGGING)) -eq 0 ]; then if [ $((($exec_time + 1) % $KEEP_LOGGING)) -eq 0 ]; then
if [ $log_ttime -ne $exec_time ]; then if [ $log_ttime -ne $exec_time ]; then
log_ttime=$exec_time log_ttime=$exec_time
Logger "Current task still running." "NOTICE" Logger "Current task still running with pid [$pid]." "NOTICE"
fi fi
fi fi
if [ $exec_time -gt $soft_max_time ]; then if [ $exec_time -gt $soft_max_time ]; then
if [ $soft_alert -eq 0 ] && [ $soft_max_time -ne 0 ]; then if [ $soft_alert -eq 0 ] && [ $soft_max_time -ne 0 ]; then
Logger "Max soft execution time exceeded for task [$caller_name]." "WARN" Logger "Max soft execution time exceeded for task [$caller_name] with pid [$pid]." "WARN"
soft_alert=1 soft_alert=1
SendAlert SendAlert
fi fi
if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then if [ $exec_time -gt $hard_max_time ] && [ $hard_max_time -ne 0 ]; then
Logger "Max hard execution time exceeded for task [$caller_name]. Stopping task execution." "ERROR" Logger "Max hard execution time exceeded for task [$caller_name] with pid [$pid]. Stopping task execution." "ERROR"
kill -s SIGTERM $pid KillChilds $pid
if [ $? == 0 ]; then if [ $? == 0 ]; then
Logger "Task stopped succesfully" "NOTICE" Logger "Task stopped successfully" "NOTICE"
return 0
else else
Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR" return 1
sleep 5 && kill -9 $pid
if [ $? != 0 ]; then
Logger "Could not stop task." "ERROR"
fi
fi fi
return 1 #kill -s SIGTERM $pid
#if [ $? == 0 ]; then
# Logger "Task stopped succesfully" "NOTICE"
#else
# Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR"
# sleep 5 && kill -9 $pid
# if [ $? != 0 ]; then
# Logger "Could not stop task." "ERROR"
# fi
#fi
#return 1
fi fi
fi fi
sleep $SLEEP_TIME sleep $SLEEP_TIME
@ -567,17 +583,24 @@ function WaitForCompletion {
fi fi
if [ $SECONDS -gt $hard_max_time ] && [ $hard_max_time != 0 ]; then if [ $SECONDS -gt $hard_max_time ] && [ $hard_max_time != 0 ]; then
Logger "Max hard execution time exceeded for script in [$caller_name]. Stopping current task execution." "ERROR" Logger "Max hard execution time exceeded for script in [$caller_name]. Stopping current task execution." "ERROR"
kill -s SIGTERM $pid KillChilds $pid
if [ $? == 0 ]; then if [ $? == 0 ]; then
Logger "Task stopped succesfully" "NOTICE" Logger "Task stopped successfully" "NOTICE"
return 0
else else
Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR" return 1
kill -9 $pid
if [ $? != 0 ]; then
Logger "Could not stop task." "ERROR"
fi
fi fi
return 1 #kill -s SIGTERM $pid
#if [ $? == 0 ]; then
# Logger "Task stopped succesfully" "NOTICE"
#else
# Logger "Sending SIGTERM to proces failed. Trying the hard way." "ERROR"
# kill -9 $pid
# if [ $? != 0 ]; then
# Logger "Could not stop task." "ERROR"
# fi
#fi
#return 1
fi fi
fi fi
sleep $SLEEP_TIME sleep $SLEEP_TIME