d3pd
d3pd

Reputation: 8315

How can I get a Python program to kill itself using a command run through the module sys?

I want to see how a Python program could kill itself by issuing a command using the module sys. How could I get it to kill itself using a command of the following form?:

os.system(killCommand)

EDIT: emphasising for clarity

So, just to be clear, I want to have a string that gets run in the shell that kills the Python program.

Upvotes: 13

Views: 42793

Answers (4)

iron coder
iron coder

Reputation: 81

If you are in the main process (i.e. Your mail python script) you can use os module

import os
import signal

#Your Python code
os.kill(os.getpid(),signal.SIGKILL)

Upvotes: 6

You can use sys.exit() to exit the program normally.

Exit the interpreter by raising SystemExit(status). If the status is omitted or None, it defaults to zero (i.e., success). If the status is an integer, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (i.e., failure).


The system command to kill the interpreter itself depends on the shell used; if your shell is bash or zsh, you can use:

a@host:~$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('kill $PPID')
Terminated
a@host:~$

Though your actual results may vary. To be safer, you need to provide the process ID yourself:

>>> os.system('kill %d' % os.getpid())

If you want to just a get signal sent to your process, you can also use os.kill() with the process id of your process; the process id of currently running process is available from os.getpid():

a@host:~$  python
Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.kill(os.getpid(), 9)
[1]    27248 killed     python
a@host:~$ 

Upvotes: 29

Kasravnd
Kasravnd

Reputation: 107297

Actually its what that sys.exit is for :

sys.exit([arg])

Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

Upvotes: 1

mbatchkarov
mbatchkarov

Reputation: 16049

sys.exit(1) will terminate the current program. The parameter is the exit status- anything but 0 indicates abnormal termination.

Upvotes: 2

Related Questions