From cf5f71ba15c897cd5768ea0dd644d1c9a6a4d4e2 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 14 Aug 2011 14:29:19 +0000 Subject: [PATCH] LRN: gnunet janitor -- another killall variant --- contrib/Makefile.am | 13 ++++---- contrib/gnunet_janitor.py.in | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 contrib/gnunet_janitor.py.in diff --git a/contrib/Makefile.am b/contrib/Makefile.am index b8dae1bbf..7ad8e542a 100644 --- a/contrib/Makefile.am +++ b/contrib/Makefile.am @@ -7,7 +7,8 @@ timeout_watchdog_SOURCES = \ endif noinst_SCRIPTS = \ - gnunet_pyexpect.py + gnunet_pyexpect.py \ + gnunet_janitor.py dist_pkgdata_DATA = \ gnunet-logo-color.png \ @@ -18,14 +19,14 @@ EXTRA_DIST = \ hostlist.cgi \ hostlist.php \ report.sh \ - gnunet_pyexpect.py.in + gnunet_pyexpect.py.in \ + gnunet_janitor.py.in do_subst = $(SED) -e 's,[@]PYTHON[@],$(PYTHON),g' -gnunet_pyexpect.py: gnunet_pyexpect.py.in Makefile - $(do_subst) < $(srcdir)/gnunet_pyexpect.py.in > gnunet_pyexpect.py - chmod +x gnunet_pyexpect.py - +%.py: %.py.in Makefile + $(do_subst) < $(srcdir)/$< > $@ + chmod +x $@ # init_gnunet_redhat \ # init_gnunet_ubuntu \ diff --git a/contrib/gnunet_janitor.py.in b/contrib/gnunet_janitor.py.in new file mode 100644 index 000000000..c23cc2858 --- /dev/null +++ b/contrib/gnunet_janitor.py.in @@ -0,0 +1,62 @@ +#!@PYTHON@ +# This file is part of GNUnet. +# (C) 2011 Christian Grothoff (and other contributing authors) +# +# GNUnet is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; either version 2, or (at your +# option) any later version. +# +# GNUnet is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNUnet; see the file COPYING. If not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. +# +# Finds any gnunet processes still running in the system and kills them +from __future__ import print_function +import os +import re +import subprocess +import sys +import shutil +import time +import signal + +if os.name == 'nt': + from win32com.client import GetObject + WMI = GetObject('winmgmts:') + +def get_process_list (): + result = [] + if os.name == 'nt': + processes = WMI.InstancesOf('Win32_Process') + for p in processes: + result.append ((p.Properties_('ProcessId').Value, re.sub (r'(.+)\.exe', r'\1', p.Properties_('Name').Value))) + else: + pids = [pid for pid in os.listdir('/proc') if pid.isdigit ()] + for pid in pids: + result.append ((pid, open (os.path.join ('/proc', pid, 'comm'), 'rb').read () + return result + +def main (): + procs = get_process_list () + gnunet_procs = [] + for p in procs: + if re.match (r'gnunet-.+', p[1]): + gnunet_procs.append (p) + for p in gnunet_procs: + if re.match (r'gnunet-service-arm', p[1]): + print ("killing arm {0:5} {1}".format (p[0], p[1])) + os.kill (p[0], signal.SIGTERM) + for p in gnunet_procs: + if not re.match (r'gnunet-service-arm', p[1]): + print ("killing arm {0:5} {1}".format (p[0], p[1])) + os.kill (p[0], signal.SIGTERM) + +if __name__ == '__main__': + sys.exit (main ()) \ No newline at end of file -- 2.25.1