Skip to content

Commit 6175bb0

Browse files
committed
Exit gracefully when receiving SIGTERM.
1 parent 852f4b0 commit 6175bb0

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ optional arguments:
9494
"psstat.csv".
9595
```
9696
97-
To terminate the monitor, send `SIGINT` signal to the process.
97+
To terminate the monitor, send `SIGINT` or `SIGTERM` signal to the process.
9898
9999
# Example
100100

resmon/resmon.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import argparse
1919
import os
2020
import sched
21+
import signal
2122
import sys
22-
import threading
2323
import time
2424
import psutil
2525

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

248248

249+
def sigterm(signum, frame):
250+
raise KeyboardInterrupt()
251+
252+
249253
def main():
250254
parser = argparse.ArgumentParser(
251255
description='Monitor system-wide resource availability. Optionally monitor processes that match the specified criteria and their children.')
@@ -281,6 +285,8 @@ def main():
281285
# Convert to lowercase to achieve case IN-sensitiveness.
282286
args.ps_keywords = [k.lower() for k in args.ps_keywords]
283287

288+
signal.signal(signal.SIGTERM, sigterm)
289+
284290
try:
285291
chprio(-20)
286292
scheduler = sched.scheduler(time.time, time.sleep)

0 commit comments

Comments
 (0)