Skip to content

Commit

Permalink
Exit gracefully when receiving SIGTERM.
Browse files Browse the repository at this point in the history
  • Loading branch information
xybu committed Dec 8, 2016
1 parent 852f4b0 commit 6175bb0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ optional arguments:
"psstat.csv".
```
To terminate the monitor, send `SIGINT` signal to the process.
To terminate the monitor, send `SIGINT` or `SIGTERM` signal to the process.
# Example
Expand Down
8 changes: 7 additions & 1 deletion resmon/resmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import argparse
import os
import sched
import signal
import sys
import threading
import time
import psutil

Expand Down Expand Up @@ -246,6 +246,10 @@ def chprio(prio):
print('Warning: failed to elevate priority!', file=sys.stderr)


def sigterm(signum, frame):
raise KeyboardInterrupt()


def main():
parser = argparse.ArgumentParser(
description='Monitor system-wide resource availability. Optionally monitor processes that match the specified criteria and their children.')
Expand Down Expand Up @@ -281,6 +285,8 @@ def main():
# Convert to lowercase to achieve case IN-sensitiveness.
args.ps_keywords = [k.lower() for k in args.ps_keywords]

signal.signal(signal.SIGTERM, sigterm)

try:
chprio(-20)
scheduler = sched.scheduler(time.time, time.sleep)
Expand Down

0 comments on commit 6175bb0

Please sign in to comment.