Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Vasudev Ram: How to kill yourself in Python

$
0
0
By Vasudev Ram

Here's a simple Python program that shows how the os.kill function from Python's standard library, along with the os.getpid function and the signal module [1], can be used to terminate the current program - the one it is called from:
'''
Program: test_self_kill.py
A program to show that the os.kill function
can be used to terminate the current program.
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
https://vasudevram.github.io
http://jugad2.blogspot.com
https://gumroad.com/vasudevram
'''

from __future__ import print_function
import sys, os, signal

print("Python version:", sys.version)
print("This line will be printed.")
os.kill(os.getpid(), signal.SIGTERM)
print("If os.kill works, this line will not be printed.")
Program output when run in Python 2.7:
$ python test_self_kill.py
Python version: 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:40:30) [MSC v.1500
64 bit (AMD64)]
This line will be printed.
Program output when run in Python 3.6:
$ python test_self_kill.py
Python version: 3.6.0a2 (v3.6.0a2:378893423552, Jun 14 2016, 01:21:40) [MSC v.19
00 64 bit (AMD64)]
This line will be printed.
As you can see, the second call to the print function does not run, because the program terminates itself.
You can read about Unix signals here and here.

- Vasudev Ram - Online Python training and consulting

.gumroad-follow-form-embed { zoom: 1; } .gumroad-follow-form-embed:before, .gumroad-follow-form-embed:after { display: table; line-height: 0; content: ""; } .gumroad-follow-form-embed:after { clear: both; } .gumroad-follow-form-embed * { margin: 0; border: 0; padding: 0; outline: 0; box-sizing: border-box !important; float: left !important; } .gumroad-follow-form-embed input { border-radius: 4px; border-top-right-radius: 0; border-bottom-right-radius: 0; font-family: -apple-system, ".SFNSDisplay-Regular", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 15px; line-height: 20px; background: #fff; border: 1px solid #ddd; border-right: 0; color: #aaa; padding: 10px; box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.02); background-position: top right; background-repeat: no-repeat; text-rendering: optimizeLegibility; font-smoothing: antialiased; -webkit-appearance: none; -moz-appearance: caret; width: 65% !important; height: 40px !important; } .gumroad-follow-form-embed button { border-radius: 4px; border-top-left-radius: 0; border-bottom-left-radius: 0; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.12); -webkit-transition: all .05s ease-in-out; transition: all .05s ease-in-out; display: inline-block; padding: 11px 15px 12px; cursor: pointer; color: #fff; font-size: 15px; line-height: 100%; font-family: -apple-system, ".SFNSDisplay-Regular", "Helvetica Neue", Helvetica, Arial, sans-serif; background: #36a9ae; border: 1px solid #31989d; filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#5ccfd4, endColorstr=#329ca1, GradientType=0)"; background: -webkit-linear-gradient(top, #5ccfd4, #329ca1); background: linear-gradient(to bottom, #5ccfd4, #329ca1); height: 40px !important; width: 35% !important; }


My Python posts     Subscribe to my blog by email

My ActiveState recipes




Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>