handle signals

This commit is contained in:
Alexey Avramov 2019-04-23 02:33:40 +09:00
parent 9e6aafce89
commit bd05de7e7f
2 changed files with 39 additions and 12 deletions

49
nohang
View File

@ -6,13 +6,16 @@ from ctypes import CDLL
from time import sleep, time
from operator import itemgetter
from sys import stdout, stderr, argv, exit, version
from signal import SIGKILL, SIGTERM
from signal import (signal,
SIGKILL, SIGTERM, SIGINT, SIGQUIT,
SIGHUP, SIGABRT, SIGSEGV, SIGBUS)
from re import search
from sre_constants import error as invalid_re
start_time = time()
help_mess = """usage: nohang [-h] [-v] [-t] [-p] [-c CONFIG]
optional arguments:
@ -84,6 +87,16 @@ cgroup_v1_index, cgroup_v2_index = find_cgroup_indexes()
# define functions
def signal_handler(sig, frame):
log('Got signal {}'.format(sig))
update_stat_dict_and_print(None)
log('Exit')
stdout.flush()
exit()
def write(path, string):
"""
"""
@ -607,14 +620,21 @@ def mlockall():
def update_stat_dict_and_print(key):
"""
"""
if key not in stat_dict:
stat_dict.update({key: 1})
else:
new_value = stat_dict[key] + 1
stat_dict.update({key: new_value})
if key is not None:
if key not in stat_dict:
stat_dict.update({key: 1})
else:
new_value = stat_dict[key] + 1
stat_dict.update({key: new_value})
if print_total_stat:
@ -1655,11 +1675,7 @@ def sleep_after_check_mem():
except OSError: # OSError: [Errno 105] No buffer space available
pass
try:
sleep(t)
except KeyboardInterrupt:
exit(1)
sleep(t)
def calculate_percent(arg_key):
"""
@ -2654,6 +2670,17 @@ if print_mem_check_results:
report0 = 0
signal(SIGTERM, signal_handler)
signal(SIGINT, signal_handler)
signal(SIGQUIT, signal_handler)
signal(SIGHUP, signal_handler)
signal(SIGABRT, signal_handler)
signal(SIGSEGV, signal_handler)
signal(SIGBUS, signal_handler)
while True:
if psi_support and not ignore_psi:

View File

@ -271,7 +271,7 @@ zram_max_warnings = 40 %
Valid values are floating-point numbers from the range [1; 300].
min_time_between_warnings = 60
min_time_between_warnings = 15
Ampersands (&) will be replaced with asterisks (*) in process
names and in commands.