def kill(pidfile, killsignal=SIG, succmsg="", errmsg="", restart_file=SERVER_RESTART, restart=False): """ Send a kill signal to a process based on PID. A customized success/error message will be returned. If clean=True, the system will attempt to manually remove the pid file. Args: pidfile (str): The path of the pidfile to get the PID from. killsignal (int, optional): Signal identifier for signal to send. succmsg (str, optional): Message to log on success. errmsg (str, optional): Message to log on failure. restart_file (str, optional): Restart file location. restart (bool, optional): Are we in restart mode or not. """ pid = get_pid(pidfile) if pid: if os.name == 'nt': os.remove(pidfile) # set restart/norestart flag if restart: django.core.management.call_command('collectstatic', interactive=False, verbosity=0) with open(restart_file, 'w') as f: f.write("reload") else: with open(restart_file, 'w') as f: f.write("shutdown") try: if os.name == 'nt': from win32api import GenerateConsoleCtrlEvent, SetConsoleCtrlHandler try: # Windows can only send a SIGINT-like signal to # *every* process spawned off the same console, so we must # avoid killing ourselves here. SetConsoleCtrlHandler(None, True) GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) except KeyboardInterrupt: # We must catch and ignore the interrupt sent. pass else: # Linux can send the SIGINT signal directly # to the specified PID. os.kill(int(pid), killsignal) except OSError: print("Process %(pid)s cannot be stopped. "\ "The PID file 'server/%(pidfile)s' seems stale. "\ "Try removing it." % {'pid': pid, 'pidfile': pidfile}) return print("Evennia:", succmsg) return print("Evennia:", errmsg)
def _generate_sigint(self): with pytest.raises(KeyboardInterrupt): if sys.platform == "win32": from win32api import GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent(0, 0) # send Ctrl+C to current TTY else: os.kill(0, signal.SIGINT) time.sleep(1)
def shutdown_server(): try: CTRL_C_EVENT = 0 GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) return "Worked" except Exception as e: # force quit os._exit(0) return e
def shutdown_server(): try: # Emulate a CTRL+C event to stop code function CTRL_C_EVENT = 0 # Value storage GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) # Uses pywin32 library return "Worked" except Exception as e: # In case of failure, make the operating system shut things down os._exit(0) # This will probably throw a nasty error, but it WILL work return e
def _generate_sigint(self): try: if sys.platform == "win32": from win32api import GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent(0, 0) # send Ctrl+C to current TTY else: os.kill(0, signal.SIGINT) time.sleep(1) except KeyboardInterrupt: pass else: self.fail("Expected KeyboardInterrupt")
# Send SIGINT in Windows using Python from win32api import GenerateConsoleCtrlEvent GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)