You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

52 lines
1.8 KiB

  1. #!/usr/bin/env python3
  2. from nilmdb.client.httpclient import HTTPClient, ClientError, ServerError
  3. from nilmdb.utils.printf import *
  4. import nilmrun
  5. import argparse
  6. import os
  7. import sys
  8. def main():
  9. """Kill/remove a process from the NilmRun server"""
  10. def_url = os.environ.get("NILMRUN_URL", "http://localhost/nilmrun/")
  11. parser = argparse.ArgumentParser(
  12. description = 'Kill/remove a process from the NilmRun server',
  13. formatter_class = argparse.ArgumentDefaultsHelpFormatter)
  14. parser.add_argument("-v", "--version", action="version",
  15. version=nilmrun.__version__)
  16. group = parser.add_argument_group("Standard options")
  17. group.add_argument('-u', '--url',
  18. help = 'NilmRun server URL', default = def_url)
  19. group.add_argument('-n', '--noverify', action="store_true",
  20. help = 'Disable SSL certificate verification')
  21. group = parser.add_argument_group("Program")
  22. group.add_argument('-q', '--quiet', action="store_true",
  23. help = "Don't print out the final log contents")
  24. group.add_argument('pid', nargs='+', help="PIDs to kill")
  25. args = parser.parse_args()
  26. client = HTTPClient(baseurl = args.url, verify_ssl = not args.noverify)
  27. # Kill or remove process
  28. all_failed = True
  29. for pid in args.pid:
  30. try:
  31. s = client.post("process/remove", { "pid": pid })
  32. if not args.quiet:
  33. sys.stdout.write(s['log'])
  34. all_failed = False
  35. except ClientError as e:
  36. if "404" in e.status:
  37. fprintf(sys.stderr, "no such pid: %s\n", pid)
  38. else:
  39. raise
  40. # Return error if we failed to remove any of them
  41. if all_failed:
  42. raise SystemExit(1)
  43. if __name__ == "__main__":
  44. main()