Skip to content

Instantly share code, notes, and snippets.

@moyix
Created January 30, 2022 17:47
Show Gist options
  • Save moyix/a234bd85c14517e18b4fc4df0dc417f3 to your computer and use it in GitHub Desktop.
Save moyix/a234bd85c14517e18b4fc4df0dc417f3 to your computer and use it in GitHub Desktop.
Script that kills any Github Actions Runner worker that has been running longer than 5 minutes
#!/usr/bin/env python3
import psutil
import docker
from datetime import timedelta, datetime
import time
client = docker.from_env()
while True:
time.sleep(10)
try:
c = client.containers.get('ephemeral-github-actions-runner.service')
except docker.errors.NotFound as e:
continue
try:
for UID, PID, PPID, C, STIME, TTY, TIME, CMD in c.top()['Processes']:
if 'Runner.Worker' in CMD:
p = psutil.Process(int(PID))
elapsed = datetime.now() - datetime.fromtimestamp(p.create_time())
if elapsed > timedelta(minutes=5):
print(f"[{datetime.now()}] Found runner job older than 5 minutes ({elapsed}): {UID} {PID} {PPID} {C} {STIME} {TTY} {TIME} {CMD}")
print("Killing...")
p.terminate()
except docker.errors.APIError as e:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment