Skip to content

Instantly share code, notes, and snippets.

@jhadev
Forked from gubatron/kill_old_ssh_agents
Created November 21, 2022 17:49
Show Gist options
  • Save jhadev/c30ba18e07d5fcc7f5dc2e5df576f134 to your computer and use it in GitHub Desktop.
Save jhadev/c30ba18e07d5fcc7f5dc2e5df576f134 to your computer and use it in GitHub Desktop.
Linux command (script) to kill old ssh-agent processes, (could be parametrized to change the process name)
#!/usr/bin/env python
# Author: @gubatron
# License: MIT License, Copyright 2019
import os
import sys
if __name__ == '__main__':
cmd_get_process_list = 'ps -ef --sort=start_time | grep ssh-agent | grep -v grep'
output = os.popen(cmd_get_process_list)
lines = output.read().split("\n")
output.close()
if len(lines) < 3:
print('kill_old_ssh_agents: You\'re good, nothing to kill')
sys.exit(0)
for line in lines[:-2]:
cmd = 'kill -9 ' + line.split()[1]
print('kill_old_ssh_agents: ' + cmd)
os.system(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment