Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #!/usr/bin/env python3.6
  2. from sys import argv, exit
  3.  
  4.  
  5. def start_stop(process):
  6. """Starts/Stops a process
  7.  
  8. It stops the process that is passed to it by killing it's process ID. If no
  9. process ID is found, then the process is started.
  10. """
  11. from subprocess import CalledProcessError, check_output, os, signal
  12.  
  13. root = 0 # root always runs with a guid of 0
  14. user = os.getuid() # who is running this script
  15.  
  16. # Ensure that we are running as root
  17. if user == root:
  18. # Attempt to kill the process
  19. try:
  20. pid_list = map(int, check_output(["pidof", process]).split())
  21. for pid in pid_list:
  22. os.kill(pid, signal.SIGKILL)
  23. print(f'TERMINATED: [{pid}] {process}')
  24. except CalledProcessError:
  25. # If the process is not running, start it
  26. print(f'STARTING: {process}:')
  27. os.system(process)
  28. else:
  29. usage()
  30.  
  31.  
  32. def usage():
  33. # Let the user know that this script needs to run as root
  34. print('Use sudo to run this script!\n')
  35. if len(argv) > 1:
  36. print(f' sudo {argv[0]} {argv[1]}\n')
  37. else:
  38. print(f' sudo {argv[0]}\n')
  39. exit()
  40.  
  41.  
  42. def main():
  43. """Run as a script
  44.  
  45. Takes the commandline argument for the process to start/stop.
  46. """
  47. if len(argv) == 1:
  48. usage()
  49. else:
  50. start_stop_proc(argv[1])
  51.  
  52.  
  53. if __name__ == '__main__':
  54. # Start the script if it is being ran from the command line
  55. start_stop("motion")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement