Skip to content

Commit

Permalink
Add signal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
roger530-ho committed May 26, 2023
1 parent ef68883 commit bc73896
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ DefaultDependencies=no

[Service]
ExecStart=/usr/local/bin/accton_as9726_32d_monitor_fan.py
KillSignal=SIGKILL
SuccessExitStatus=SIGKILL

# Resource Limitations
LimitCORE=infinity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ DefaultDependencies=no

[Service]
ExecStart=/usr/local/bin/accton_as9726_32d_monitor_psu.py
KillSignal=SIGKILL
SuccessExitStatus=SIGKILL

# Resource Limitations
LimitCORE=infinity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ DefaultDependencies=no
[Service]
ExecStartPre=/usr/local/bin/accton_as9726_32d_util.py install
ExecStart=/usr/local/bin/accton_as9726_32d_monitor.py
KillSignal=SIGKILL
SuccessExitStatus=SIGKILL

# Resource Limitations
LimitCORE=infinity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import logging
import logging.config
import logging.handlers
import signal
import time # this is only being used as part of the example
except ImportError as e:
raise ImportError('%s - required module not found' % str(e))
Expand Down Expand Up @@ -59,6 +60,9 @@ def match(self, *args):
fan_state=[2, 2, 2, 2, 2, 2] #init state=2, insert=1, remove=0
fan_status_state=[2, 2, 2, 2, 2, 2] #init state=2, fault=1, normal=0
# Make a class we can use to capture stdout and sterr in the log

exit_by_sigterm=0

class device_monitor(object):

def __init__(self, log_file, log_level):
Expand Down Expand Up @@ -160,9 +164,20 @@ def manage_fan(self):

return True

def signal_handler(sig, frame):
global exit_by_sigterm
if sig == signal.SIGTERM:
print("Caught SIGTERM - exiting...")
exit_by_sigterm = 1
else:
pass

def main(argv):
log_file = '%s.log' % FUNCTION_NAME
log_level = logging.INFO
global exit_by_sigterm
signal.signal(signal.SIGTERM, signal_handler)

if len(sys.argv) != 1:
try:
opts, args = getopt.getopt(argv,'hdl:',['lfile='])
Expand All @@ -181,6 +196,8 @@ def main(argv):
while True:
monitor.manage_fan()
time.sleep(3)
if exit_by_sigterm == 1:
break

if __name__ == '__main__':
main(sys.argv[1:])
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import logging
import logging.config
import logging.handlers
import signal
import time # this is only being used as part of the example
except ImportError as e:
raise ImportError('%s - required module not found' % str(e))
Expand All @@ -37,6 +38,9 @@

psu_state=[2, 2]
psu_status_state=[2, 2]

exit_by_sigterm=0

# Make a class we can use to capture stdout and sterr in the log
class device_monitor(object):

Expand Down Expand Up @@ -132,9 +136,19 @@ def manage_psu(self):

return True

def signal_handler(sig, frame):
global exit_by_sigterm
if sig == signal.SIGTERM:
print("Caught SIGTERM - exiting...")
exit_by_sigterm = 1
else:
pass

def main(argv):
log_file = '%s.log' % FUNCTION_NAME
log_level = logging.INFO
global exit_by_sigterm
signal.signal(signal.SIGTERM, signal_handler)
if len(sys.argv) != 1:
try:
opts, args = getopt.getopt(argv,'hdl:',['lfile='])
Expand All @@ -154,6 +168,8 @@ def main(argv):
while True:
monitor.manage_psu()
time.sleep(3)
if exit_by_sigterm == 1:
break

if __name__ == '__main__':
main(sys.argv[1:])

0 comments on commit bc73896

Please sign in to comment.